/**
 *====================================================
 * 文件名称: LoanCastor.java
 * 修订记录：
 * No    日期				作者(操作:具体内容)
 * 1.    2015年11月26日			chenxy(创建:创建文件)
 *====================================================
 * 类描述：(说明未实现或其它不应生成javadoc的内容)
 */
package com.yuexunit.finance.project.action.v1.dto;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yuexunit.core.ConfigPropertites;
import com.yuexunit.finance.bulkstock.entity.BulkstockInfoEntity;
import com.yuexunit.finance.common.util.BeanUtils;
import com.yuexunit.finance.common.util.DateUtils;
import com.yuexunit.finance.context.FinanceConstant;
import com.yuexunit.finance.context.FinanceSessionContext;
import com.yuexunit.finance.member.entity.EnterpriseEntity;
import com.yuexunit.finance.member.entity.EnterpriseGradeEntity;
import com.yuexunit.finance.member.entity.MonthEarningDetailEntity;
import com.yuexunit.finance.member.entity.SupplierEntity;
import com.yuexunit.finance.member.entity.lexicon.AuditStateEnum;
import com.yuexunit.finance.member.service.EnterpriseGradeService;
import com.yuexunit.finance.member.service.EnterpriseService;
import com.yuexunit.finance.member.service.SupplierService;
import com.yuexunit.finance.project.action.v1.FinanceBulkstockActionApi;
import com.yuexunit.finance.project.action.v1.model.FinanceBulkstock;
import com.yuexunit.finance.project.action.v1.model.FinanceMoneyStatement;
import com.yuexunit.finance.project.action.v1.model.FinanceMoneyStatementDetails;
import com.yuexunit.finance.project.action.v1.model.LoanNotice;
import com.yuexunit.finance.project.action.v1.model.Receivables;
import com.yuexunit.finance.project.action.v1.model.input.FinanceWriteoffParams;
import com.yuexunit.finance.project.action.v1.model.output.Capital;
import com.yuexunit.finance.project.action.v1.model.output.ContainerInventory;
import com.yuexunit.finance.project.action.v1.model.output.FinanceAuditDetail;
import com.yuexunit.finance.project.action.v1.model.output.FinanceCreditBulkstockDetail;
import com.yuexunit.finance.project.action.v1.model.output.FinanceCreditDetail;
import com.yuexunit.finance.project.action.v1.model.output.FinanceCreditLineInfo;
import com.yuexunit.finance.project.action.v1.model.output.FinanceFilesInfoDetail;
import com.yuexunit.finance.project.action.v1.model.output.FinanceWriteoffDetail;
import com.yuexunit.finance.project.action.v1.model.output.FinanceWriteoffLineInfo;
import com.yuexunit.finance.project.action.v1.model.output.InterestDelayDetail;
import com.yuexunit.finance.project.action.v1.model.output.InterestDelayLineInfo;
import com.yuexunit.finance.project.action.v1.model.output.LoanNoticeDetail;
import com.yuexunit.finance.project.action.v1.model.output.LoanNoticeLineInfo;
import com.yuexunit.finance.project.action.v1.model.output.QuarterSettlementContainerDetail;
import com.yuexunit.finance.project.action.v1.model.output.QuarterSettlementLineInfo;
import com.yuexunit.finance.project.action.v1.model.output.ReceivablesDetail;
import com.yuexunit.finance.project.action.v1.model.output.ReceivablesLineInfo;
import com.yuexunit.finance.project.entity.AbnormalEntity;
import com.yuexunit.finance.project.entity.AbnormalExpenseEntity;
import com.yuexunit.finance.project.entity.BailReturnEntity;
import com.yuexunit.finance.project.entity.BillReceivableEntity;
import com.yuexunit.finance.project.entity.ClaimEntity;
import com.yuexunit.finance.project.entity.ClaimSettlementEntity;
import com.yuexunit.finance.project.entity.ContainerBulkstockEntity;
import com.yuexunit.finance.project.entity.ContainerInventoryEntity;
import com.yuexunit.finance.project.entity.FinanceApplicationEntity;
import com.yuexunit.finance.project.entity.FinanceAuditEntity;
import com.yuexunit.finance.project.entity.FinanceBulkstockEntity;
import com.yuexunit.finance.project.entity.FinanceContractEntity;
import com.yuexunit.finance.project.entity.FinanceCreditBulkstockEntity;
import com.yuexunit.finance.project.entity.FinanceCreditContainerBulkstockEntity;
import com.yuexunit.finance.project.entity.FinanceCreditContainerEntity;
import com.yuexunit.finance.project.entity.FinanceCreditEntity;
import com.yuexunit.finance.project.entity.FinanceCreditReplenishEntity;
import com.yuexunit.finance.project.entity.FinanceFilesInfoEntity;
import com.yuexunit.finance.project.entity.FinanceMoneyStatementDetailsEntity;
import com.yuexunit.finance.project.entity.FinanceMoneyStatementEntity;
import com.yuexunit.finance.project.entity.FinanceOtherMoneyStatementEntity;
import com.yuexunit.finance.project.entity.FinanceProductEntity;
import com.yuexunit.finance.project.entity.FinanceProductExpenseEntity;
import com.yuexunit.finance.project.entity.FinanceProductInnerEntity;
import com.yuexunit.finance.project.entity.FinanceProductInterestItemEntity;
import com.yuexunit.finance.project.entity.FinanceWriteoffEntity;
import com.yuexunit.finance.project.entity.FinanceWriteoffStatementEntity;
import com.yuexunit.finance.project.entity.InsuranceEntity;
import com.yuexunit.finance.project.entity.InterestDelayApplicationEntity;
import com.yuexunit.finance.project.entity.LoanNoticeEntity;
import com.yuexunit.finance.project.entity.PickupApplicationEntity;
import com.yuexunit.finance.project.entity.PickupApplicationWarehouseEntity;
import com.yuexunit.finance.project.entity.PickupBulkstockWarehouseEntity;
import com.yuexunit.finance.project.entity.QuarterSettlementContainerEntity;
import com.yuexunit.finance.project.entity.QuarterSettlementEntity;
import com.yuexunit.finance.project.entity.ReceivablesEntity;
import com.yuexunit.finance.project.entity.lexicon.AdjustmentEnum;
import com.yuexunit.finance.project.entity.lexicon.ApiFinanceStateEnum;
import com.yuexunit.finance.project.entity.lexicon.BeneficiaryMethodEnum;
import com.yuexunit.finance.project.entity.lexicon.BillTypeEnum;
import com.yuexunit.finance.project.entity.lexicon.BillingMethodEnum;
import com.yuexunit.finance.project.entity.lexicon.ExpenseTypeEnum;
import com.yuexunit.finance.project.entity.lexicon.LoanMethodEnum;
import com.yuexunit.finance.project.entity.lexicon.OperatorRoleEnum;
import com.yuexunit.finance.project.entity.lexicon.ProductTypeEnum;
import com.yuexunit.finance.project.entity.lexicon.ReplenishEnum;
import com.yuexunit.finance.project.entity.lexicon.ReplenishMethodEnum;
import com.yuexunit.finance.project.entity.lexicon.ReplenishStateEnum;
import com.yuexunit.finance.project.entity.lexicon.StatementStateEnum;
import com.yuexunit.finance.project.entity.lexicon.SupervisionStateEnum;
import com.yuexunit.finance.project.entity.lexicon.WriteoffTypeEnum;
import com.yuexunit.finance.project.service.ContainerBulkstockService;
import com.yuexunit.finance.project.service.ContainerInventoryService;
import com.yuexunit.finance.project.service.FinanceApplicationService;
import com.yuexunit.finance.project.service.FinanceAuditService;
import com.yuexunit.finance.project.service.FinanceBulkstockService;
import com.yuexunit.finance.project.service.FinanceContractService;
import com.yuexunit.finance.project.service.FinanceCreditBulkstockService;
import com.yuexunit.finance.project.service.FinanceCreditReplenishService;
import com.yuexunit.finance.project.service.FinanceFilesInfoService;
import com.yuexunit.finance.project.service.FinanceMoneyStatementDetailsService;
import com.yuexunit.finance.project.service.FinanceMoneyStatementService;
import com.yuexunit.finance.project.service.FinanceProductExpenseService;
import com.yuexunit.finance.project.service.FinanceProductInnerService;
import com.yuexunit.finance.project.service.FinanceProductInterestItemService;
import com.yuexunit.finance.project.service.FinanceProductService;
import com.yuexunit.finance.project.service.FinanceWriteoffStatementService;
import com.yuexunit.finance.project.service.LoanNoticeService;
import com.yuexunit.finance.project.service.PickupApplicationService;
import com.yuexunit.finance.project.service.PickupApplicationWarehouseService;
import com.yuexunit.finance.system.entity.SignFileInfoEntity;
import com.yuexunit.finance.system.service.SignFileInfoService;
import com.yuexunit.inject.ComponentFactory;
import com.yuexunit.permission.action.PermissionActionsApi;
import com.yuexunit.permission.action.dto.RoleListDto;
import com.yuexunit.util.DateUtil;
import com.yuexunit.util.StringUtil;

/**
 * @ClassName: LoanCastor
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author chenxy
 * @date 2015年11月26日 下午10:28:10
 */
public class FinanceLoanCastor {

	/** 固定计息产品 */
	private static QuarterSettlementEntity caculateFixedProduct(FinanceApplicationEntity finance, List<ContainerInventoryEntity> inventories, Integer days, PickupApplicationEntity pickup, boolean updateContainer) {
		EnterpriseGradeService enterpriseGradeService = ComponentFactory.loadClass(EnterpriseGradeService.class);
		EnterpriseGradeEntity grade = enterpriseGradeService.inquireEnterpriseGrade(finance.getEnterpriseId());
		QuarterSettlementEntity entity = new QuarterSettlementEntity();
		entity.setFinanceId(finance.getFinanceId());
		entity.setFinanceNumber(finance.getFinanceNumber());
		entity.setEnterpriseId(finance.getEnterpriseId());
		entity.setEnterpriseName(finance.getEnterpriseName());
		if (pickup != null) {
			entity.setPickupId(pickup.getPickupId());
			entity.setSettlementType(2);
		} else {
			entity.setDays(days);
			entity.setSettlementType(1);
		}
		entity.setCreatorId(FinanceSessionContext.getAccountId());
		entity.setCreator(FinanceSessionContext.getAccountFullName());

		BigDecimal financeAmount = BigDecimal.ZERO;
		BigDecimal interestAmount = BigDecimal.ZERO;
		BigDecimal interestAmountOrigin = BigDecimal.ZERO;
		BigDecimal punishAmount = BigDecimal.ZERO;
		BigDecimal interest = finance.getInterest().divide(FinanceConstant.HUNDRED, 10, BigDecimal.ROUND_HALF_UP);
		if (grade != null && grade.getInterestRate() != null) {
			interest = (finance.getInterest().divide(FinanceConstant.HUNDRED, 10, BigDecimal.ROUND_HALF_UP)).multiply(grade.getInterestRate().divide(FinanceConstant.HUNDRED, 10, BigDecimal.ROUND_HALF_UP));
		} 
		
		BigDecimal interestOrigin = finance.getInterest().divide(FinanceConstant.HUNDRED, 10, BigDecimal.ROUND_HALF_UP);
		Integer loanTimeLimit = finance.getLoanTimeLimit();

		ContainerBulkstockService containerBulkstockService = ComponentFactory.loadClass(ContainerBulkstockService.class);
		FinanceProductExpenseService financeProductExpenseService = ComponentFactory.loadClass(FinanceProductExpenseService.class);
		List<QuarterSettlementContainerEntity> settlementContainers = new ArrayList<>();
		for (ContainerInventoryEntity cie : inventories) {
			if (SupervisionStateEnum.ALREADY_SUPERVISE != cie.getSupervisionState() || cie.getValueDate() == null) {
				continue;
			}
			BigDecimal financeContainerAmount = BigDecimal.ZERO;
			BigDecimal interestContainerAmount = BigDecimal.ZERO;
			BigDecimal interestContainerAmountOrigin = BigDecimal.ZERO;
			BigDecimal punishContainerAmount = BigDecimal.ZERO;
			Date settlementBeginDate = cie.getSettlementDate();
			Date settlementEndDate = null;
			if (pickup != null) { // 提货结息
//				settlementEndDate = DateUtils.fromDateInt(pickup.getDeliveryDate());
//				Date punishDate = cie.getPunishDate();
//				if (settlementEndDate.getTime() < punishDate.getTime()) { // 罚息前提货配送，则收取整单利息
//					settlementEndDate = DateUtils.addDateField(punishDate, Calendar.DATE, -1);
//				}
				settlementEndDate = DateUtils.addDateField(settlementBeginDate, Calendar.DATE, ((int)(Math.ceil((DateUtils.daysBetween(cie.getSettlementDate(), DateUtils.fromDateInt(pickup.getDeliveryDate())))*1.0/days))) * days);
			} else { // 阶段结息
				settlementEndDate = DateUtils.addDateField(settlementBeginDate, Calendar.DATE, days); // 已结算日期
			}
			int interestDays = 0;
			int between = 0; // 罚息天数
			if (settlementEndDate.getTime() >= cie.getPunishDate().getTime()) { // 超过提货期限,进入罚息期
				between = DateUtils.daysBetween(cie.getPunishDate(), settlementEndDate) + 1;
				interestDays = DateUtils.daysBetween(settlementBeginDate, cie.getPunishDate()); // 箱的正常计息天数
			} else {
				interestDays = DateUtils.daysBetween(settlementBeginDate, settlementEndDate);
			}
			// 获取箱货信息
			List<ContainerBulkstockEntity> bulkstocks = containerBulkstockService.inquireContainerBulkstockList(cie.getInventoryId());
			FinanceProductExpenseEntity expense = null; // 差价按第一个货计算费率
			for (ContainerBulkstockEntity cbe : bulkstocks) {
				BigDecimal financeBulkstockAmount = cbe.getValuationFinanceAmount();
				BigDecimal interestContainerBulkstockAmount = BigDecimal.ZERO;
				BigDecimal interestContainerBulkstockAmountOrigin = BigDecimal.ZERO;
				BigDecimal punishContainerBulkstockAmount = BigDecimal.ZERO;
				if (between > 0) { // 查询罚息利息
					expense = financeProductExpenseService.inquireFinanceProductExpense(finance.getProductId(), cbe.getBulkstockCategoryId(), cbe.getBulkstockInfoId(), between, ExpenseTypeEnum.DELAY);
					if (expense == null) {
//						throw new BusinessException(cie.getCaseNumber() + "超过产品制定的罚息期限,无法计算利息");
						punishContainerBulkstockAmount = BigDecimal.ZERO;
					} else {
						// 货物金额*罚息利率/100*罚息天数
						punishContainerBulkstockAmount = financeBulkstockAmount.multiply(expense.getAmount().divide(FinanceConstant.HUNDRED, 10, BigDecimal.ROUND_HALF_UP)).multiply(new BigDecimal(between));
					}
				}
				// 按单计算
				interestContainerBulkstockAmount = financeBulkstockAmount.multiply(interest).multiply(new BigDecimal(interestDays).divide(new BigDecimal(loanTimeLimit), 10, BigDecimal.ROUND_HALF_UP));
				interestContainerBulkstockAmountOrigin = financeBulkstockAmount.multiply(interestOrigin).multiply(new BigDecimal(interestDays).divide(new BigDecimal(loanTimeLimit), 10, BigDecimal.ROUND_HALF_UP));
				financeContainerAmount = financeContainerAmount.add(financeBulkstockAmount);
				interestContainerAmount = interestContainerAmount.add(interestContainerBulkstockAmount);
				interestContainerAmountOrigin = interestContainerAmountOrigin.add(interestContainerBulkstockAmountOrigin);
				punishContainerAmount = punishContainerAmount.add(punishContainerBulkstockAmount);
			}
			if (updateContainer) {
				cie.setSettlementDate(settlementEndDate);
			}
			// 计算箱补价利息及罚息
			financeContainerAmount = financeContainerAmount.add(cie.getAdjustment());
			interestContainerAmount = interestContainerAmount.add(cie.getAdjustment().multiply(interest).multiply(new BigDecimal(interestDays).divide(new BigDecimal(loanTimeLimit), 10, BigDecimal.ROUND_HALF_UP)));
			interestContainerAmountOrigin = interestContainerAmountOrigin.add(cie.getAdjustment().multiply(interestOrigin).multiply(new BigDecimal(interestDays).divide(new BigDecimal(loanTimeLimit), 10, BigDecimal.ROUND_HALF_UP)));
			if (expense != null) {
				punishContainerAmount = punishContainerAmount.add(cie.getAdjustment().multiply(expense.getAmount()).multiply(new BigDecimal(between)));
			}

			// 收息箱信息
			QuarterSettlementContainerEntity container = new QuarterSettlementContainerEntity();
			container.setContainerInventory(cie);
			container.setInventoryId(cie.getInventoryId());
			container.setBeginDateTime(settlementBeginDate);
			container.setEndDateTime(settlementEndDate);
			container.setFinanceAmount(financeContainerAmount);
			container.setInterestAmount(interestContainerAmount);
			container.setPunishAmount(punishContainerAmount);
			settlementContainers.add(container);

			financeAmount = financeAmount.add(container.getFinanceAmount());
			punishAmount = punishAmount.add(container.getPunishAmount());
			interestAmount = interestAmount.add(container.getInterestAmount());
			interestAmountOrigin = interestAmountOrigin.add(interestContainerAmountOrigin);
		}
		entity.setFinanceAmount(financeAmount);
		entity.setInterestAmount(interestAmount.setScale(2, BigDecimal.ROUND_HALF_UP));
		entity.setPunishAmount(punishAmount.setScale(2, BigDecimal.ROUND_HALF_UP));
		entity.setSettlementAmount(entity.getInterestAmount().add(entity.getPunishAmount()));
		entity.setSettlementAmountOrigin(interestAmountOrigin.setScale(2, BigDecimal.ROUND_HALF_UP));
		entity.setContainers(settlementContainers);
		return entity;
	}

	/** 按单计息产品 */
	private static QuarterSettlementEntity caculateDaysProduct(FinanceProductEntity product, FinanceApplicationEntity finance, List<ContainerInventoryEntity> inventories, Integer days, PickupApplicationEntity pickup,
			boolean updateContainer) {
		EnterpriseGradeService enterpriseGradeService = ComponentFactory.loadClass(EnterpriseGradeService.class);
		EnterpriseGradeEntity grade = enterpriseGradeService.inquireEnterpriseGrade(finance.getEnterpriseId());
		QuarterSettlementEntity entity = new QuarterSettlementEntity();
		entity.setFinanceId(finance.getFinanceId());
		entity.setFinanceNumber(finance.getFinanceNumber());
		entity.setEnterpriseId(finance.getEnterpriseId());
		entity.setEnterpriseName(finance.getEnterpriseName());
		if (pickup != null) {
			entity.setPickupId(pickup.getPickupId());
			entity.setSettlementType(2);
		} else {
			entity.setDays(days);
			entity.setSettlementType(1);
		}
		BigDecimal financeAmount = BigDecimal.ZERO;
		BigDecimal interestAmount = BigDecimal.ZERO;
		BigDecimal interestAmountOrigin = BigDecimal.ZERO;
		BigDecimal punishAmount = BigDecimal.ZERO;
		BigDecimal interest = finance.getInterest().divide(FinanceConstant.HUNDRED, 10, BigDecimal.ROUND_HALF_UP);
		if (grade != null && grade.getInterestRate() != null) {
			interest = (finance.getInterest().divide(FinanceConstant.HUNDRED, 10, BigDecimal.ROUND_HALF_UP)).multiply(grade.getInterestRate().divide(FinanceConstant.HUNDRED, 10, BigDecimal.ROUND_HALF_UP));
		}
		BigDecimal interestOrigin = finance.getInterest().divide(FinanceConstant.HUNDRED, 10, BigDecimal.ROUND_HALF_UP);
		Integer baseDays = product.getBaseDays() == null ? 0 : product.getBaseDays();// 基础天数

		ContainerBulkstockService containerBulkstockService = ComponentFactory.loadClass(ContainerBulkstockService.class);
		List<QuarterSettlementContainerEntity> settlementContainers = new ArrayList<>();
		for (ContainerInventoryEntity cie : inventories) {
			if (SupervisionStateEnum.ALREADY_SUPERVISE != cie.getSupervisionState() || cie.getValueDate() == null) {
				continue;
			}
			BigDecimal financeContainerAmount = BigDecimal.ZERO;
			BigDecimal interestContainerAmount = BigDecimal.ZERO;
			BigDecimal interestContainerAmountOrigin = BigDecimal.ZERO;
			BigDecimal punishContainerAmount = BigDecimal.ZERO;
			Date valueDate = cie.getValueDate();
			Date settlementBeginDate = cie.getSettlementDate();
			Date settlementEndDate = null;
			if (pickup != null) { // 提货结息
				settlementEndDate = DateUtils.fromDateInt(pickup.getDeliveryDate());
				Date punishDate = cie.getPunishDate();
				if (settlementEndDate.getTime() < punishDate.getTime()) { // 罚息前提货配送，检查是否小于基础天数
					Date baseValueDate = DateUtils.addDateField(valueDate, Calendar.DATE, baseDays);
					if (baseValueDate.after(settlementEndDate)) {
						settlementEndDate = baseValueDate;
					}
				}
			} else { // 阶段结息
				settlementEndDate = DateUtils.addDateField(settlementBeginDate, Calendar.DATE, days); // 已结算日期
			}
			int interestDays = 0;
			int between = 0; // 罚息天数
			if (settlementEndDate.getTime() >= cie.getPunishDate().getTime()) { // 超过提货期限,进入罚息期
				between = DateUtils.daysBetween(cie.getPunishDate(), settlementEndDate) + 1;
				interestDays = DateUtils.daysBetween(settlementBeginDate, cie.getPunishDate()); // 箱的正常计息天数
			} else {
				interestDays = DateUtils.daysBetween(settlementBeginDate, settlementEndDate);
			}
			// 获取箱货信息
			List<ContainerBulkstockEntity> bulkstocks = containerBulkstockService.inquireContainerBulkstockList(cie.getInventoryId());
//			FinanceProductExpenseEntity expense = null; // 差价按第一个货计算费率
			for (ContainerBulkstockEntity cbe : bulkstocks) {
				BigDecimal financeBulkstockAmount = cbe.getValuationFinanceAmount();
				BigDecimal interestContainerBulkstockAmount = BigDecimal.ZERO;
				BigDecimal interestContainerBulkstockAmountOrigin = BigDecimal.ZERO;
				BigDecimal punishContainerBulkstockAmount = BigDecimal.ZERO;
				if (between > 0) { // 查询罚息利息
//					expense = financeProductExpenseService.inquireFinanceProductExpense(finance.getProductId(), cbe.getBulkstockCategoryId(), cbe.getBulkstockInfoId(), between, ExpenseTypeEnum.DELAY);
//					if (expense == null) {
//						throw new BusinessException(cie.getCaseNumber() + "超过产品制定的罚息期限,无法计算利息");
//					}
//					// 货物金额*罚息利率/100*罚息天数
//					punishContainerBulkstockAmount = financeBulkstockAmount.multiply(expense.getAmount().divide(FinanceConstant.HUNDRED, 10, BigDecimal.ROUND_HALF_UP)).multiply(new BigDecimal(between));
				}
				// 按单计算
				interestContainerBulkstockAmount = financeBulkstockAmount.multiply(interest).multiply(new BigDecimal(interestDays)); // 日息*天数
				interestContainerBulkstockAmountOrigin = financeBulkstockAmount.multiply(interestOrigin).multiply(new BigDecimal(interestDays)); // 日息*天数

				financeContainerAmount = financeContainerAmount.add(financeBulkstockAmount);
				interestContainerAmount = interestContainerAmount.add(interestContainerBulkstockAmount);
				interestContainerAmountOrigin = interestContainerAmountOrigin.add(interestContainerBulkstockAmountOrigin);
				punishContainerAmount = punishContainerAmount.add(punishContainerBulkstockAmount);
			}
			if (updateContainer) {
				cie.setSettlementDate(settlementEndDate);
			}
			// 计算箱补价利息及罚息
			financeContainerAmount = financeContainerAmount.add(cie.getAdjustment());
			interestContainerAmount = interestContainerAmount.add(cie.getAdjustment().multiply(interest).multiply(new BigDecimal(interestDays)));
			interestContainerAmountOrigin = interestContainerAmountOrigin.add(cie.getAdjustment().multiply(interestOrigin).multiply(new BigDecimal(interestDays)));
//			if (expense != null) {
//				punishContainerAmount = punishContainerAmount.add(cie.getAdjustment().multiply(expense.getAmount()).multiply(new BigDecimal(between)));
//			}

			// 收息箱信息
			QuarterSettlementContainerEntity container = new QuarterSettlementContainerEntity();
			container.setContainerInventory(cie);
			container.setInventoryId(cie.getInventoryId());
			container.setBeginDateTime(settlementBeginDate);
			container.setEndDateTime(settlementEndDate);
			container.setFinanceAmount(financeContainerAmount);
			container.setInterestAmount(interestContainerAmount);
			container.setPunishAmount(punishContainerAmount);
			settlementContainers.add(container);

			financeAmount = financeAmount.add(container.getFinanceAmount());
			punishAmount = punishAmount.add(container.getPunishAmount());
			interestAmount = interestAmount.add(container.getInterestAmount());
			interestAmountOrigin = interestAmountOrigin.add(interestContainerAmountOrigin);
			
		}

		entity.setFinanceAmount(financeAmount);
		entity.setInterestAmount(interestAmount.setScale(2, BigDecimal.ROUND_HALF_UP));
		entity.setPunishAmount(punishAmount.setScale(2, BigDecimal.ROUND_HALF_UP));
		entity.setSettlementAmount(entity.getInterestAmount().add(entity.getPunishAmount()));
		entity.setSettlementAmountOrigin(interestAmountOrigin);
		entity.setContainers(settlementContainers);
		return entity;
	}

	/** 创建阶段结息 */
	public static QuarterSettlementEntity fromInterestSettlement(FinanceApplicationEntity finance, List<ContainerInventoryEntity> inventories, Integer days, PickupApplicationEntity pickup, boolean updateContainer) {
		FinanceProductService financeProductService = ComponentFactory.loadClass(FinanceProductService.class);
		FinanceProductEntity product = financeProductService.inquireFinanceProduct(finance.getProductId());
		if (LoanMethodEnum.FIXED_LOAN == product.getLoanMethod()) {
			return caculateFixedProduct(finance, inventories, days, pickup, updateContainer);
		} else if (LoanMethodEnum.DAY_LOAN == product.getLoanMethod()) {
			return caculateDaysProduct(product, finance, inventories, days, pickup, updateContainer);
		}
		return null;
	}
	
	/** 创建阶段结息-改 */
	public static QuarterSettlementEntity fromInterestSettlement1(FinanceApplicationEntity finance, List<ContainerInventoryEntity> inventories, Integer days, PickupApplicationEntity pickup, boolean updateContainer) {
		return caculateFixedProduct1(finance, inventories, days, pickup, updateContainer);
	}
	
	/** 计息产品 */
	private static QuarterSettlementEntity caculateFixedProduct1(FinanceApplicationEntity finance, List<ContainerInventoryEntity> inventories, Integer days, PickupApplicationEntity pickup, boolean updateContainer) {
		EnterpriseGradeService enterpriseGradeService = ComponentFactory.loadClass(EnterpriseGradeService.class);
		ContainerInventoryService containerInventoryService = ComponentFactory.loadClass(ContainerInventoryService.class);
		EnterpriseGradeEntity grade = enterpriseGradeService.inquireEnterpriseGrade(finance.getEnterpriseId());
		FinanceProductInnerService financeProductInnerService = ComponentFactory.loadClass(FinanceProductInnerService.class);
		FinanceProductInterestItemService financeProductInterestItemService = ComponentFactory.loadClass(FinanceProductInterestItemService.class);
		ContainerBulkstockService containerBulkstockService = ComponentFactory.loadClass(ContainerBulkstockService.class);
//		QuarterSettlementContainerService quarterSettlementContainerService = ComponentFactory.loadClass(QuarterSettlementContainerService.class);
		QuarterSettlementEntity entity = new QuarterSettlementEntity();
		entity.setFinanceId(finance.getFinanceId());
		entity.setFinanceNumber(finance.getFinanceNumber());
		entity.setEnterpriseId(finance.getEnterpriseId());
		entity.setEnterpriseName(finance.getEnterpriseName());
		if (pickup != null) {
			entity.setPickupId(pickup.getPickupId());
			entity.setSettlementType(2);
		} else {
			entity.setDays(days);
			entity.setSettlementType(1);
		}
		if (FinanceSessionContext.getAccountId() != null) {
			entity.setCreatorId(FinanceSessionContext.getAccountId());
			entity.setCreator(FinanceSessionContext.getAccountFullName());
		}

		BigDecimal financeAmount = BigDecimal.ZERO;
		BigDecimal interestAmount = BigDecimal.ZERO;
		BigDecimal interestAmountOrigin = BigDecimal.ZERO;
		BigDecimal punishAmount = BigDecimal.ZERO;
		
		FinanceProductInnerEntity productInner = financeProductInnerService.inquireFinanceProductInner(finance.getFinanceId(), null);
		if(productInner==null)
			return null;
		List<FinanceProductInterestItemEntity> fpiies = financeProductInterestItemService.inquireFinanceProductInterestItemList(productInner.getFinanceProductId());
		
		BigDecimal interest = BigDecimal.ZERO;
		BigDecimal interestOrigin = BigDecimal.ZERO;
//		Integer loanTimeLimit = productInner.getFinanceDays();
		List<QuarterSettlementContainerEntity> settlementContainers = new ArrayList<>();
		
		for (ContainerInventoryEntity cie : inventories) {
			if (SupervisionStateEnum.ALREADY_SUPERVISE != cie.getSupervisionState() || cie.getValueDate() == null) {
				continue;
			}
			
			BigDecimal financeContainerAmount = BigDecimal.ZERO;
			BigDecimal interestContainerAmount = BigDecimal.ZERO;
			BigDecimal interestContainerAmountOrigin = BigDecimal.ZERO;
			
			Date settlementBeginDate = cie.getSettlementDate();
			Date settlementEndDate = null;
			if (pickup != null) { // 提货结息
				settlementEndDate = DateUtils.addDateField(settlementBeginDate, Calendar.DATE, DateUtils.daysBetween(cie.getSettlementDate(), DateUtils.fromDateInt(pickup.getDeliveryDate())));
			} else { // 阶段结息
				settlementEndDate = DateUtils.addDateField(settlementBeginDate, Calendar.DATE, DateUtils.daysBetween(cie.getSettlementDate(), DateUtils.now())); // 已结算日期
			}
			int interestDays = DateUtils.daysBetween(settlementBeginDate, settlementEndDate);
			// 获取箱货信息
			List<ContainerBulkstockEntity> bulkstocks = containerBulkstockService.inquireContainerBulkstockList(cie.getInventoryId());
			for (ContainerBulkstockEntity cbe : bulkstocks) {
				System.out.println(interestContainerAmount);
				BigDecimal financeBulkstockAmount = cbe.getValuationFinanceAmount().add(cie.getAdjustment());
				BigDecimal interestContainerBulkstockAmount = BigDecimal.ZERO;
				BigDecimal interestContainerBulkstockAmountOrigin = BigDecimal.ZERO;
				for (FinanceProductInterestItemEntity fpiie : fpiies) {
					int d = DateUtils.daysBetween(cie.getValueDate(), DateUtils.fromDateInt(pickup.getDeliveryDate())) + 1;
					interest = fpiie.getAmout().divide(FinanceConstant.HUNDRED, 10, BigDecimal.ROUND_HALF_UP);
					interestOrigin = fpiie.getAmout().divide(FinanceConstant.HUNDRED, 10, BigDecimal.ROUND_HALF_UP);
					if (grade != null && grade.getInterestRate() != null) {
						interest = (fpiie.getAmout().divide(FinanceConstant.HUNDRED, 10, BigDecimal.ROUND_HALF_UP)).multiply(grade.getInterestRate().divide(FinanceConstant.HUNDRED, 10, BigDecimal.ROUND_HALF_UP));
					}
					if (d >= fpiie.getStartDay() && d < fpiie.getEndDay()) {
						if (fpiie.getBillingMethod() == BillingMethodEnum.DAYS) {
							interest = interest.multiply(BigDecimal.valueOf(interestDays));
							interestOrigin = interestOrigin.multiply(BigDecimal.valueOf(interestDays));
						} else if (fpiie.getBillingMethod() == BillingMethodEnum.EXPENSE) {
							interest = interest.multiply(BigDecimal.valueOf(fpiie.getEndDay() - fpiie.getStartDay() + 1));
							interestOrigin = interestOrigin.multiply(BigDecimal.valueOf(fpiie.getEndDay() - fpiie.getStartDay() + 1));
							settlementEndDate = DateUtils.fromDateInt(pickup.getDeliveryDate());
						}
					} else if (d >= fpiie.getEndDay()) {
						interestDays = interestDays - (fpiie.getEndDay() - fpiie.getStartDay() + 1);
						interest = interest.multiply(BigDecimal.valueOf(fpiie.getEndDay() - fpiie.getStartDay() + 1));
						interestOrigin = interestOrigin.multiply(BigDecimal.valueOf(fpiie.getEndDay() - fpiie.getStartDay() + 1));
					} else {
						continue;
					}
					interestContainerBulkstockAmount = interestContainerBulkstockAmount.add(financeBulkstockAmount.multiply(interest));
					interestContainerBulkstockAmountOrigin = interestContainerBulkstockAmountOrigin.add(financeBulkstockAmount.multiply(interestOrigin));
				}
				
				financeContainerAmount = financeContainerAmount.add(financeBulkstockAmount);
				interestContainerAmount = interestContainerAmount.add(interestContainerBulkstockAmount);
				interestContainerAmountOrigin = interestContainerAmountOrigin.add(interestContainerBulkstockAmountOrigin);
			}
			if (updateContainer) {
				cie.setSettlementDate(settlementEndDate);
				containerInventoryService.updateContainerInventory(cie);
			}
			// 计算箱补价利息及罚息
//			financeContainerAmount = financeContainerAmount.add(BigDecimal.ZERO);
//			interestContainerAmount = interestContainerAmount.add(cie.getAdjustment().multiply(interest).multiply(new BigDecimal(interestDays).divide(new BigDecimal(loanTimeLimit), 10, BigDecimal.ROUND_HALF_UP)));
//			interestContainerAmountOrigin = interestContainerAmountOrigin.add(cie.getAdjustment().multiply(interestOrigin).multiply(new BigDecimal(interestDays).divide(new BigDecimal(loanTimeLimit), 10, BigDecimal.ROUND_HALF_UP)));

			// 收息箱信息
			QuarterSettlementContainerEntity container = new QuarterSettlementContainerEntity();
			container.setContainerInventory(cie);
			container.setInventoryId(cie.getInventoryId());
			container.setBeginDateTime(settlementBeginDate);
			container.setEndDateTime(settlementEndDate);
			container.setFinanceAmount(financeContainerAmount);
			container.setInterestAmount(interestContainerAmount);
			container.setPunishAmount(BigDecimal.ZERO);
			settlementContainers.add(container);

			financeAmount = financeAmount.add(container.getFinanceAmount());
			interestAmount = interestAmount.add(container.getInterestAmount());
			interestAmountOrigin = interestAmountOrigin.add(interestContainerAmountOrigin);
			
//			if (updateContainer && container.getInterestAmount().compareTo(BigDecimal.ZERO) > 0) {
//				quarterSettlementContainerService.createQuarterSettlementContainer(container);
//			}
		}
		entity.setFinanceAmount(financeAmount);
		entity.setInterestAmount(interestAmount.setScale(2, BigDecimal.ROUND_HALF_UP));
		entity.setPunishAmount(punishAmount.setScale(2, BigDecimal.ROUND_HALF_UP));
		entity.setSettlementAmount(entity.getInterestAmount().add(entity.getPunishAmount()));
		entity.setSettlementAmountOrigin(interestAmountOrigin.setScale(2, BigDecimal.ROUND_HALF_UP));
		entity.setContainers(settlementContainers);
		return entity;
	}

	public static QuarterSettlementLineInfo toSettlementLineInfo(QuarterSettlementEntity entity) {
		QuarterSettlementLineInfo info = new QuarterSettlementLineInfo();
		BeanUtils.copyProperties(entity, info);
		List<QuarterSettlementContainerDetail> containDetailList = new ArrayList<QuarterSettlementContainerDetail>();
		if (entity.getContainers() != null) {
			for (QuarterSettlementContainerEntity qsc : entity.getContainers()) {
				QuarterSettlementContainerDetail detail = new QuarterSettlementContainerDetail();
				BeanUtils.copyProperties(qsc, detail);
				containDetailList.add(detail);
			}
		}
		info.setContainers(containDetailList);
		return info;
	}
	// ====================================================================================================================================================//

	public static InterestDelayDetail toInterestDelayDetail(InterestDelayApplicationEntity entity) {
		InterestDelayDetail detail = new InterestDelayDetail();
		BeanUtils.copyProperties(entity, detail);
		detail.setApplicationState(entity.getApplicationState().ordinal());
		detail.setApplicantDatetime(entity.getApplicantDatetime());
		return detail;
	}

	public static InterestDelayLineInfo toInterestDelayLineInfo(InterestDelayApplicationEntity entity) {
		InterestDelayLineInfo info = new InterestDelayLineInfo();
		BeanUtils.copyProperties(entity, info);
		info.setApplicationState(entity.getApplicationState().ordinal());
		return info;
	}

	// ====================================================================================================================================================//
	public static LoanNoticeEntity fromLoanNotice(LoanNotice notice, FinanceApplicationEntity finance) {
		PermissionActionsApi permissionAction = ComponentFactory.loadClass(PermissionActionsApi.class);
		List<RoleListDto> rl = permissionAction.inquireAccountRoleList(FinanceSessionContext.getAccountId());
		StringBuffer sb = new StringBuffer();
		for (RoleListDto rlt : rl) {
			sb.append(rlt.getRoleName()).append(",");
		}
		sb.deleteCharAt(sb.length() - 1);
		LoanNoticeEntity entity = new LoanNoticeEntity();
		BeanUtils.copyProperties(notice, entity);
		entity.setLoanDate(DateUtil.parseDateInt(notice.getLoanDate()));
		entity.setFinanceNumber(finance.getFinanceNumber());
		entity.setEnterpriseId(finance.getEnterpriseId());
		entity.setEnterpriseName(finance.getEnterpriseName());
		entity.setOperator(FinanceSessionContext.getAccountFullName());
		entity.setOperatorId(FinanceSessionContext.getAccountId());
		entity.setLoanMethod(BeneficiaryMethodEnum.valueOf(notice.getLoanMethod()));
		entity.setApplicant(FinanceSessionContext.getAccountFullName());
		entity.setApplyDate(DateUtil.now());
		entity.setApplyDepartment(sb.toString());
		entity.setFinalState(ApiFinanceStateEnum.ZJ.getCode());
		//运营-7
		entity.setAuditState1(Boolean.TRUE);
		//运营领导-8
		entity.setAuditState2(Boolean.TRUE);
		//财务-3
		entity.setFinancialAuditState(Boolean.TRUE);
		//财务领导-4
		entity.setFinancialLeaderAuditState(Boolean.TRUE);
		//总经理-5
		entity.setLeader2AuditState(Boolean.TRUE);
		return entity;
	}

	public static LoanNoticeDetail toLoanNoticeDetail(LoanNoticeEntity entity) {
		FinanceBulkstockActionApi financeBulkstockActionApi = ComponentFactory.loadClass(FinanceBulkstockActionApi.class);
		LoanNoticeDetail detail = new LoanNoticeDetail();
		BeanUtils.copyProperties(entity, detail);
		detail.setLoanMethod(entity.getLoanMethod().ordinal());
		detail.setLoanDate(DateUtil.formatDate(entity.getLoanDate()));
		detail.setLoanState(entity.getLoanState().ordinal());
		
		List<ContainerInventory> resultList = financeBulkstockActionApi.inquireFinanceContainerInventoryList(null, null, entity.getFinanceId(), entity.getLoanNoticeId(), null);
		detail.setContainerInventorys(resultList);
		
//		ContainerInventoryService containerInventoryService = ComponentFactory.loadClass(ContainerInventoryService.class);
//		List<ContainerInventoryEntity> inventories = containerInventoryService.inquireLoanNoticeContainerInventoryList(entity.getLoanNoticeId());
//		for (ContainerInventoryEntity cie : inventories) {
//			detail.getContainerInventorys().add(BulkstockCastor.toContainerInventory(cie, Boolean.FALSE));
//		}
		
		FinanceFilesInfoService financeFilesInfoService = ComponentFactory.loadClass(FinanceFilesInfoService.class);
		FinanceAuditService financeAuditService = ComponentFactory.loadClass(FinanceAuditService.class);
		FinanceApplicationService financeApplicationService = ComponentFactory.loadClass(FinanceApplicationService.class);
		FinanceApplicationEntity finance = financeApplicationService.inquireFinanceApplicationEntity(entity.getFinanceId());
		SignFileInfoService signFileInfoService = ComponentFactory.loadClass(SignFileInfoService.class);
		
		detail.setProductType(finance.getProductType().ordinal());
		FinanceAuditEntity fae = financeAuditService.inquireLatestFinanceAudit(entity.getFinanceId());
		FinanceAuditDetail fad = FinanceApplicationCastor.toFinanceAuditDetail(fae);
		detail.setFinanceAuditDetail(fad);
		
		//融资附件列表
		List<FinanceFilesInfoEntity> ffies = financeFilesInfoService.inquireFinanceFilesInfoList(entity.getFinanceId(), null);
		List<FinanceFilesInfoDetail> ffids = new ArrayList<>();
		for (FinanceFilesInfoEntity ffie : ffies) {
			if (StringUtil.isNotBlank(ffie.getFilesUuid())) {
				FinanceFilesInfoDetail ffid = new FinanceFilesInfoDetail();
				BeanUtils.copyProperties(ffie, ffid);
				StringBuffer sb = new StringBuffer();
				for (String s : ffie.getFilesUuid().split(",")) {
					SignFileInfoEntity sfie = signFileInfoService.inquireSignFileInfo(s);
					if (sfie != null) {
						sb.append(sfie.getFileSignState().ordinal() + ",");
					}
				}
				if (sb != null && sb.length() > 0) {
					sb = sb.deleteCharAt(sb.length() - 1);
				}
				ffid.setFileSignState(sb.toString());
				ffids.add(ffid);
			}
		}
		detail.setFinanceFilesInfoList(ffids);
		return detail;
	}

	public static LoanNoticeLineInfo toLoanNoticeLineInfo(LoanNoticeEntity entity) {
		FinanceContractService financeContractService = ComponentFactory.loadClass(FinanceContractService.class);
		FinanceApplicationService financeApplicationService = ComponentFactory.loadClass(FinanceApplicationService.class);
		FinanceBulkstockService financeBulkstockService = ComponentFactory.loadClass(FinanceBulkstockService.class);
		
		LoanNoticeLineInfo info = new LoanNoticeLineInfo();
		FinanceApplicationEntity finance = financeApplicationService.inquireFinanceApplicationEntity(entity.getFinanceId());
		
		BeanUtils.copyProperties(entity, info);
		info.setLoanMethod(entity.getLoanMethod().ordinal());
		info.setLoanDate(DateUtil.formatDate(entity.getLoanDate()));
		info.setLoanState(entity.getLoanState().ordinal());
		info.setProductType(finance.getProductType().ordinal());
		if(entity.getFinalState() !=null){
			info.setFinalState(entity.getFinalState().intValue());
			info.setFinalStateView((ApiFinanceStateEnum.getByValue(entity.getFinalState())).getView());
		}
//		if(entity.getApplicant() !=null && !"".equals(entity.getApplicant())){
//			info.setApplicant(entity.getApplicant());
//		}
		List<FinanceBulkstockEntity> fbes = financeBulkstockService.inquireFinanceBulkstockList(entity.getFinanceId());
		info.setBulkstockName(fbes.get(0).getBulkstockName());
		for (FinanceBulkstockEntity fbe : fbes) {
			FinanceBulkstock fb = new FinanceBulkstock();
			BeanUtils.copyProperties(fbe, fb);
		}
		
		FinanceContractEntity fce = financeContractService.inquireFinanceIdContract(entity.getFinanceId(), 2);
		if (fce != null) {
			info.setFinanceContractSerialNumber(fce.getFinanceContractSerialNumber());
		}
		return info;
	}

	// ====================================================================================================================================================//
	public static ReceivablesEntity fromReceivables(PickupApplicationEntity pickupEntity, Receivables receivables) {
		if (receivables.getCreditQuota() == null) {
			receivables.setCreditQuota(BigDecimal.ZERO);
		}
		ReceivablesEntity entity = new ReceivablesEntity();
		BeanUtils.copyProperties(receivables, entity);
		entity.setFinanceId(pickupEntity.getFinanceId());
		entity.setFinanceNumber(pickupEntity.getFinanceNumber());
		entity.setEnterpriseId(pickupEntity.getEnterpriseId());
		entity.setEnterpriseName(pickupEntity.getEnterpriseName());
		entity.setPickupNumber(pickupEntity.getPickupNumber());
		entity.setBeneficiaryDate(DateUtil.parseDateInt(receivables.getBeneficiaryDate()));
		entity.setBeneficiaryMethod(BeneficiaryMethodEnum.valueOf(receivables.getBeneficiaryMethod()));
		entity.setOperator(FinanceSessionContext.getAccountFullName());
		entity.setOperatorId(FinanceSessionContext.getAccountId());
		return entity;
	}

	public static ReceivablesDetail toReceivablesDetail(ReceivablesEntity entity) {
		ReceivablesDetail detail = new ReceivablesDetail();
		BeanUtils.copyProperties(entity, detail);
		detail.setBeneficiaryMethod(entity.getBeneficiaryMethod().ordinal());
		detail.setBeneficiaryDate(DateUtil.formatDate(entity.getBeneficiaryDate()));
		detail.setReceivablesState(entity.getReceivablesState().ordinal());
		return detail;
	}

	public static ReceivablesLineInfo toReceivablesLineInfo(ReceivablesEntity entity) {
		ReceivablesLineInfo info = new ReceivablesLineInfo();
		BeanUtils.copyProperties(entity, info);
		info.setBeneficiaryMethod(entity.getBeneficiaryMethod().ordinal());
		info.setBeneficiaryDate(DateUtil.formatDate(entity.getBeneficiaryDate()));
		info.setReceivablesState(entity.getReceivablesState().ordinal());
		return info;
	}

	// ====================================================================================================================================================//
	/** 融资调整信息 */
	public static FinanceCreditDetail toFinanceCreditDetail(FinanceCreditEntity entity) {
		FinanceCreditDetail info = new FinanceCreditDetail();
		FinanceApplicationService financeApplicationService = ComponentFactory.loadClass(FinanceApplicationService.class);
		BeanUtils.copyProperties(entity, info);
		FinanceCreditBulkstockService financeCreditBulkstockService = ComponentFactory.loadClass(FinanceCreditBulkstockService.class);
		List<FinanceCreditBulkstockEntity> bulkstocks = financeCreditBulkstockService.inquireFinanceCreditBulkstockList(entity.getCreditId());
		for (FinanceCreditBulkstockEntity fcbe : bulkstocks) {
			FinanceCreditBulkstockDetail t = new FinanceCreditBulkstockDetail();
			BeanUtils.copyProperties(fcbe, t);
			info.getCreditBulkstocks().add(t);
		}
		FinanceApplicationEntity finance = financeApplicationService.inquireFinanceApplicationEntity(entity.getFinanceId());
		info.setAuditDate(entity.getAuditDateTime());
		if (entity.getAuditState() != null && entity.getAuditState().ordinal() != 0) {
			info.setAuditState(entity.getAuditState().ordinal());
		} else {
			info.setAuditState(0);
		}
		info.setEnterpriseName(finance.getEnterpriseName());
		return info;
	}

	/** 融资调整信息1 */
	public static FinanceCreditDetail toFinanceCreditDetail1(FinanceCreditEntity entity) {
		FinanceCreditDetail info = new FinanceCreditDetail();
		FinanceCreditReplenishService financeCreditReplenishService = ComponentFactory.loadClass(FinanceCreditReplenishService.class);
		FinanceApplicationService financeApplicationService = ComponentFactory.loadClass(FinanceApplicationService.class);
		BeanUtils.copyProperties(entity, info);
		List<FinanceCreditReplenishEntity> replenishes = financeCreditReplenishService.inquireFinanceCreditReplenishList(entity.getCreditId());
		for (FinanceCreditReplenishEntity fcre : replenishes) {
			FinanceCreditBulkstockDetail t = new FinanceCreditBulkstockDetail();
			BeanUtils.copyProperties(fcre, t);
			info.getCreditBulkstocks().add(t);
		}
		FinanceApplicationEntity finance = financeApplicationService.inquireFinanceApplicationEntity(entity.getFinanceId());
		info.setAuditDate(entity.getAuditDateTime());
		info.setEnterpriseName(finance.getEnterpriseName());
		return info;
	}

	/** 融资调整信息 */
	public static FinanceCreditLineInfo toFinanceCreditLineInfo(FinanceCreditEntity entity) {
		FinanceCreditLineInfo info = new FinanceCreditDetail();
		FinanceApplicationService financeApplicationService = ComponentFactory.loadClass(FinanceApplicationService.class);
		BeanUtils.copyProperties(entity, info);
		FinanceApplicationEntity finance = financeApplicationService.inquireFinanceApplicationEntity(entity.getFinanceId());
		if (finance != null) {
			info.setEnterpriseName(finance.getEnterpriseName());
		}
		if (entity.getAuditState() == null) {
			info.setAuditState(0);
		} else {
			info.setAuditState(entity.getAuditState().ordinal());
		}
		return info;
	}

	// ====================================================================================================================================================//
	/** 帐户流水 */
	public static FinanceMoneyStatement toFinanceMoneyStatement(FinanceMoneyStatementEntity entity) {
		FinanceMoneyStatementDetailsService financeMoneyStatementDetailsService = ComponentFactory.loadClass(FinanceMoneyStatementDetailsService.class);
		FinanceMoneyStatement info = new FinanceMoneyStatement();
		if(entity.getFinanceId() != null){
			FinanceApplicationService financeApplicationService = ComponentFactory.loadClass(FinanceApplicationService.class);
			FinanceApplicationEntity finance = financeApplicationService.inquireFinanceApplicationEntity(entity.getFinanceId());
			if (finance == null) {
				System.out.println(entity.getFinanceId());
			} else {
				info.setProductType(finance.getProductType().ordinal());
			}
		}
		BeanUtils.copyProperties(entity, info);
		if (entity.getFinanceId() != null) {
			LoanNoticeService loanNoticeService = ComponentFactory.loadClass(LoanNoticeService.class);
			List<LoanNoticeEntity> list = loanNoticeService.inquireLoanNoticeList(entity.getFinanceId());
			if (list != null && list.size() > 0) {
				info.setLoanNoticeId(list.get(0).getLoanNoticeId());
			}
		}
		info.setPaymentChannel(entity.getPaymentChannel().ordinal());
		info.setStatementState(entity.getStatementState().ordinal());
		info.setStatementDate(DateUtil.formatDate(entity.getStatementDate()));
		List<FinanceMoneyStatementDetailsEntity> fmsdes = financeMoneyStatementDetailsService.inquireFinanceMoneyStatementDetailsList(entity.getMoneyStatementId(), null, null);
		List<FinanceMoneyStatementDetails> fmsds = new ArrayList<>();
		for (FinanceMoneyStatementDetailsEntity fmsde : fmsdes) {
			FinanceMoneyStatementDetails d = new FinanceMoneyStatementDetails();
			BeanUtils.copyProperties(fmsde, d);
			fmsds.add(d);
		}
		info.setFinanceMoneyStatementDetailsList(fmsds);
		return info;
	}

	/** 利息收缴流水 */
	public static FinanceMoneyStatementEntity fromInterestSettlement(QuarterSettlementEntity entity) {
		EnterpriseService enterpriseService = ComponentFactory.loadClass(EnterpriseService.class);
		if (entity == null) {
			return null;
		}
		EnterpriseEntity enterprise = enterpriseService.inquireEnterprise(entity.getEnterpriseId());

		FinanceMoneyStatementEntity statement = new FinanceMoneyStatementEntity();
		statement.setFinanceId(entity.getFinanceId());
		statement.setFinanceNumber(entity.getFinanceNumber());

		statement.setReceiptType(OperatorRoleEnum.PLATFORM.ordinal());
		statement.setReceiptName(ConfigPropertites.getProperties("platform.name"));
		statement.setReceiptTenantId(FinanceConstant.DEFAULT_PLATFORM_WEB_TENANT);
		statement.setDisbursementType(OperatorRoleEnum.ENTERPRISE.ordinal());
		statement.setDisbursementName(entity.getEnterpriseName());
		statement.setDisbursementId(entity.getEnterpriseId());
		statement.setDisbursementTenantId(enterprise.getTenantId());

		statement.setAmount(entity.getSettlementAmount());
		statement.setStatementState(StatementStateEnum.WAIT_PAY);
		statement.setStatementSource("QUARTER_SETTLEMENT");
		statement.setStatementSourceId(entity.getQuarterSettlementId());
		statement.setSourceNumber(entity.getSettlementNumber());
		statement.setStatementDate(DateUtil.todayInt());
		return statement;
	}

	/** 生成异常费用 */
	public static FinanceMoneyStatementEntity fromAbnormalExpense(AbnormalEntity abnormal, AbnormalExpenseEntity expense) {
		FinanceMoneyStatementEntity statement = new FinanceMoneyStatementEntity();
		statement.setFinanceId(abnormal.getFinanceId());
		statement.setFinanceNumber(abnormal.getFinanceNumber());
		statement.setReceiptType(expense.getReceiptType().ordinal());
		statement.setReceiptName(expense.getReceiptName());
		statement.setReceiptId(expense.getReceiptId());
		statement.setReceiptTenantId(expense.getReceiptTenantId());
		statement.setDisbursementType(expense.getDisbursementType().ordinal());
		statement.setDisbursementName(expense.getDisbursementName());
		statement.setDisbursementId(expense.getDisbursementId());
		statement.setDisbursementTenantId(expense.getDisbursementTenantId());
		statement.setAmount(expense.getAbnormalAmount());
		statement.setStatementState(StatementStateEnum.PAID);
		statement.setStatementSource("ABNORMAL_EXPENSE");
		statement.setStatementSourceId(expense.getAbnormalId());
		statement.setSourceNumber(expense.getVoucher());
		statement.setStatementDate(DateUtils.parseDateInt(expense.getPaymentDate()));
		return statement;
	}

	/** 融资审核保证金 */
	public static FinanceMoneyStatementEntity fromFinanceAuditBail(FinanceApplicationEntity finance, FinanceAuditEntity audit) {
		EnterpriseService enterpriseService = ComponentFactory.loadClass(EnterpriseService.class);
		EnterpriseEntity enterprise = enterpriseService.inquireEnterprise(finance.getEnterpriseId());

		FinanceMoneyStatementEntity statement = new FinanceMoneyStatementEntity();
		statement.setFinanceId(finance.getFinanceId());
		statement.setFinanceNumber(finance.getFinanceNumber());
		statement.setReceiptType(OperatorRoleEnum.PLATFORM.ordinal());
		statement.setReceiptName(ConfigPropertites.getProperties("platform.name"));
		statement.setReceiptTenantId(FinanceConstant.DEFAULT_PLATFORM_WEB_TENANT);
		statement.setDisbursementType(OperatorRoleEnum.ENTERPRISE.ordinal());
		statement.setDisbursementId(finance.getEnterpriseId());
		statement.setDisbursementTenantId(enterprise.getTenantId());
		statement.setDisbursementName(finance.getEnterpriseName());
		statement.setAmount(audit.getBail());
		statement.setStatementState(StatementStateEnum.PAID);
		statement.setStatementSource("FINANCE_AUDIT");
		statement.setStatementSourceId(audit.getFinanceAuditId());
		statement.setSourceNumber(audit.getAuditNumber());
		statement.setStatementDate(DateUtil.parseDateInt(DateUtil.formatDate(audit.getAuditDateTime())));
		return statement;
	}

	/** 融资保证金归还 */
	public static FinanceMoneyStatementEntity toFinanceBailReturn(FinanceApplicationEntity finance, BailReturnEntity bail) {
		EnterpriseService enterpriseService = ComponentFactory.loadClass(EnterpriseService.class);
		EnterpriseEntity enterprise = enterpriseService.inquireEnterprise(finance.getEnterpriseId());

		FinanceMoneyStatementEntity statement = new FinanceMoneyStatementEntity();
		statement.setFinanceId(finance.getFinanceId());
		statement.setFinanceNumber(finance.getFinanceNumber());
		statement.setReceiptType(OperatorRoleEnum.ENTERPRISE.ordinal());
		statement.setReceiptName(finance.getEnterpriseName());
		statement.setReceiptTenantId(enterprise.getTenantId());
		statement.setReceiptId(finance.getEnterpriseId());
		statement.setDisbursementType(OperatorRoleEnum.PLATFORM.ordinal());
		statement.setDisbursementTenantId(FinanceConstant.DEFAULT_PLATFORM_WEB_TENANT);
		statement.setDisbursementName(ConfigPropertites.getProperties("platform.name"));
		statement.setAmount(bail.getAmount());
		statement.setStatementState(StatementStateEnum.PAID);
		statement.setStatementSource("BAIL_RETURN");
		statement.setStatementDate(DateUtil.todayInt());
		statement.setStatementSourceId(finance.getFinanceId());
		statement.setSourceNumber(finance.getFinanceNumber());

		return statement;
	}

	/** 融资保证金追加 */
	public static FinanceMoneyStatementEntity fromFinanceAdditionBail(FinanceApplicationEntity finance, FinanceCreditEntity credit) {
		EnterpriseService enterpriseService = ComponentFactory.loadClass(EnterpriseService.class);
		EnterpriseEntity enterprise = enterpriseService.inquireEnterprise(finance.getEnterpriseId());

		FinanceMoneyStatementEntity statement = new FinanceMoneyStatementEntity();
		statement.setFinanceId(finance.getFinanceId());
		statement.setFinanceNumber(finance.getFinanceNumber());
		statement.setReceiptType(OperatorRoleEnum.PLATFORM.ordinal());
		statement.setReceiptName(ConfigPropertites.getProperties("platform.name"));
		statement.setReceiptTenantId(FinanceConstant.DEFAULT_PLATFORM_WEB_TENANT);
		statement.setDisbursementType(OperatorRoleEnum.ENTERPRISE.ordinal());
		statement.setDisbursementId(finance.getEnterpriseId());
		statement.setDisbursementTenantId(enterprise.getTenantId());
		statement.setDisbursementName(finance.getEnterpriseName());
		statement.setAmount(credit.getCreditAmount());
		statement.setStatementState(StatementStateEnum.PAID);
		statement.setStatementSource("BAIL_ADDITION");
		statement.setStatementSourceId(credit.getCreditId());
		statement.setSourceNumber(credit.getFinanceNumber());
		statement.setStatementDate(DateUtil.parseDateInt(DateUtil.formatDate(credit.getCreateDateTime())));
		return statement;
	}
	
	/** 提货申请流水-货物金额 */
	public static FinanceMoneyStatementEntity fromPickupApplication(PickupApplicationEntity pickup) {
		FinanceMoneyStatementEntity statement = new FinanceMoneyStatementEntity();
		statement.setFinanceId(pickup.getFinanceId());
		statement.setFinanceNumber(pickup.getFinanceNumber());
		statement.setReceiptType(OperatorRoleEnum.PLATFORM.ordinal());
		statement.setReceiptName(ConfigPropertites.getProperties("platform.name"));
		statement.setReceiptTenantId(FinanceConstant.DEFAULT_PLATFORM_WEB_TENANT);
		statement.setDisbursementType(OperatorRoleEnum.ENTERPRISE.ordinal());
		statement.setDisbursementName(pickup.getEnterpriseName());
		statement.setAmount(pickup.getFinanceValuation());
		statement.setStatementState(StatementStateEnum.PAID);
		statement.setStatementSource("RECEIVABLES");
		statement.setStatementSourceId(pickup.getPickupId());
		statement.setSourceNumber(pickup.getPickupNumber());
		statement.setStatementDate(DateUtil.todayInt());
		statement.setDisbursementId(pickup.getEnterpriseId());
		return statement;
	}
	
	/** 提货申请流水-货物利息 */
	public static FinanceMoneyStatementEntity fromPickupApplicationInterest(PickupApplicationEntity pickup) {
		FinanceMoneyStatementEntity statement = new FinanceMoneyStatementEntity();
		statement.setFinanceId(pickup.getFinanceId());
		statement.setFinanceNumber(pickup.getFinanceNumber());
		statement.setReceiptAmount(BigDecimal.ZERO);
		statement.setReceiptType(OperatorRoleEnum.PLATFORM.ordinal());
		statement.setReceiptName(ConfigPropertites.getProperties("platform.name"));
		statement.setReceiptTenantId(FinanceConstant.DEFAULT_PLATFORM_WEB_TENANT);
		statement.setDisbursementType(OperatorRoleEnum.ENTERPRISE.ordinal());
		statement.setDisbursementName(pickup.getEnterpriseName());
		statement.setAmount(pickup.getBulkstockInterest());
		statement.setStatementState(StatementStateEnum.PAID);
		statement.setStatementSource("QUARTER_SETTLEMENT");
		statement.setStatementSourceId(pickup.getPickupId());
		statement.setSourceNumber(pickup.getPickupNumber());
		statement.setStatementDate(DateUtil.todayInt());
		statement.setDisbursementId(pickup.getEnterpriseId());
		return statement;
	}
	
	/** 提货申请流水-货物利息 */
	public static FinanceMoneyStatementEntity fromMonthEarningDetail(MonthEarningDetailEntity entity) {
		FinanceMoneyStatementEntity statement = new FinanceMoneyStatementEntity();
		statement.setFinanceId(entity.getFinanceId());
		statement.setFinanceNumber(entity.getFinanceNumber());
		statement.setReceiptAmount(BigDecimal.ZERO);
		statement.setReceiptType(OperatorRoleEnum.PLATFORM.ordinal());
		statement.setReceiptName(ConfigPropertites.getProperties("platform.name"));
		statement.setReceiptTenantId(FinanceConstant.DEFAULT_PLATFORM_WEB_TENANT);
		statement.setDisbursementType(OperatorRoleEnum.ENTERPRISE.ordinal());
		statement.setDisbursementName(entity.getEnterpriseName());
		statement.setAmount(entity.getShouldPay());
		statement.setStatementState(StatementStateEnum.PAID);
		statement.setStatementSource("QUARTER_SETTLEMENT_MONTH");
		statement.setStatementSourceId(entity.getMonthEarningDetailId());
		statement.setSourceNumber(entity.getFinanceNumber());
		statement.setStatementDate(DateUtils.parseDateInt(entity.getSettlementEnd()));
		statement.setDisbursementId(entity.getEnterpriseId());
		return statement;
	}
	
	/** 提货申请流水-其他费用 */
	public static FinanceMoneyStatementEntity fromPickupApplicationOtherAmount(PickupApplicationEntity pickup) {
		FinanceMoneyStatementEntity statement = new FinanceMoneyStatementEntity();
		statement.setFinanceId(pickup.getFinanceId());
		statement.setFinanceNumber(pickup.getFinanceNumber());
		statement.setReceiptAmount(BigDecimal.ZERO);
		statement.setReceiptType(OperatorRoleEnum.PLATFORM.ordinal());
		statement.setReceiptName(ConfigPropertites.getProperties("platform.name"));
		statement.setReceiptTenantId(FinanceConstant.DEFAULT_PLATFORM_WEB_TENANT);
		statement.setDisbursementType(OperatorRoleEnum.ENTERPRISE.ordinal());
		statement.setDisbursementName(pickup.getEnterpriseName());
		statement.setAmount(pickup.getOtherAmount());
		statement.setStatementState(StatementStateEnum.PAID);
		statement.setStatementSource("PICKUP_OTHER_AMOUNT");
		statement.setStatementSourceId(pickup.getPickupId());
		statement.setSourceNumber(pickup.getPickupNumber());
		statement.setStatementDate(DateUtil.todayInt());
		statement.setDisbursementId(pickup.getEnterpriseId());
		return statement;
	}
	
	/** 代采代销提货申请流水-货物金额 */
	public static FinanceMoneyStatementEntity fromWarehousePickupApplication(PickupBulkstockWarehouseEntity pbwe) {
		PickupApplicationWarehouseService pickupApplicationWarehouseService = ComponentFactory.loadClass(PickupApplicationWarehouseService.class);
		PickupApplicationWarehouseEntity pawe = pickupApplicationWarehouseService.inquirePickupApplicationWarehouseDetail(pbwe.getPickupId());
		
		FinanceMoneyStatementEntity statement = new FinanceMoneyStatementEntity();
		statement.setReceiptType(OperatorRoleEnum.PLATFORM.ordinal());
		statement.setFinanceId(pbwe.getFinanceId());
		statement.setFinanceNumber(pbwe.getFinanceNumber());
		statement.setReceiptName(ConfigPropertites.getProperties("platform.name"));
		statement.setReceiptTenantId(FinanceConstant.DEFAULT_PLATFORM_WEB_TENANT);
		statement.setDisbursementType(OperatorRoleEnum.ENTERPRISE.ordinal());
		statement.setDisbursementName(pawe.getEnterpriseName());
		statement.setAmount(pbwe.getBulkstockAmount());
		statement.setStatementState(StatementStateEnum.PAID);
		statement.setStatementSource("RECEIVABLES_WAREHOUSE");
		statement.setStatementSourceId(pawe.getPickupId());
		statement.setSourceNumber(pawe.getPickupNumber());
		statement.setStatementDate(DateUtil.todayInt());
		statement.setDisbursementId(pawe.getEnterpriseId());
		return statement;
	}
	
//	/** //TODO 月账单结算-利息 */
//	public static FinanceMoneyStatementEntity fromMonthEarningDetail(MonthFinancingInfo mfi) {
//		MonthEarningService monthEarningService = ComponentFactory.loadClass(MonthEarningService.class);
//		MonthEarningEntity mee = monthEarningService.inquiryMonthEarningDetail(med.getMonthEarningId());
//		
//		FinanceMoneyStatementEntity statement = new FinanceMoneyStatementEntity();
//		statement.setReceiptType(OperatorRoleEnum.PLATFORM.ordinal());
//		statement.setFinanceId(med.getFinanceId());
//		statement.setFinanceNumber(med.getFinanceNumber());
//		statement.setReceiptName(ConfigPropertites.getProperties("platform.name"));
//		statement.setReceiptTenantId(FinanceConstant.DEFAULT_PLATFORM_WEB_TENANT);
//		statement.setDisbursementType(OperatorRoleEnum.ENTERPRISE.ordinal());
//		statement.setDisbursementName(pawe.getEnterpriseName());
//		statement.setAmount(pbwe.getBulkstockAmount());
//		statement.setStatementState(StatementStateEnum.PAID);
//		statement.setStatementSource("QUARTER_SETTLEMENT_INTEREST_MONTH");
//		statement.setStatementSourceId(pawe.getPickupId());
//		statement.setSourceNumber(pawe.getPickupNumber());
//		statement.setStatementDate(DateUtil.todayInt());
//		statement.setDisbursementId(pawe.getEnterpriseId());
//		return statement;
//	}
	
	/** 提货申请流水-货物利息 */
	public static FinanceMoneyStatementEntity fromWarehousePickupApplicationInterest(PickupBulkstockWarehouseEntity pbwe) {
		PickupApplicationWarehouseService pickupApplicationWarehouseService = ComponentFactory.loadClass(PickupApplicationWarehouseService.class);
		PickupApplicationWarehouseEntity pawe = pickupApplicationWarehouseService.inquirePickupApplicationWarehouseDetail(pbwe.getPickupId());

		FinanceMoneyStatementEntity statement = new FinanceMoneyStatementEntity();
		statement.setReceiptAmount(BigDecimal.ZERO);
		statement.setFinanceId(pbwe.getFinanceId());
		statement.setFinanceNumber(pbwe.getFinanceNumber());
		statement.setReceiptType(OperatorRoleEnum.PLATFORM.ordinal());
		statement.setReceiptName(ConfigPropertites.getProperties("platform.name"));
		statement.setReceiptTenantId(FinanceConstant.DEFAULT_PLATFORM_WEB_TENANT);
		statement.setDisbursementType(OperatorRoleEnum.ENTERPRISE.ordinal());
		statement.setDisbursementName(pawe.getEnterpriseName());
		statement.setAmount(pbwe.getInterest());
		statement.setStatementState(StatementStateEnum.PAID);
		statement.setStatementSource("QUARTER_SETTLEMENT_INTEREST");
		statement.setStatementSourceId(pawe.getPickupId());
		statement.setSourceNumber(pawe.getPickupNumber());
		statement.setStatementDate(DateUtil.todayInt());
		statement.setDisbursementId(pawe.getEnterpriseId());
		return statement;
	}
	
	/** 提货申请流水-其他费用利息 */
	public static FinanceMoneyStatementEntity fromWarehousePickupApplicationOhers(PickupBulkstockWarehouseEntity pbwe) {
		PickupApplicationWarehouseService pickupApplicationWarehouseService = ComponentFactory.loadClass(PickupApplicationWarehouseService.class);
		PickupApplicationWarehouseEntity pawe = pickupApplicationWarehouseService.inquirePickupApplicationWarehouseDetail(pbwe.getPickupId());
		
		FinanceMoneyStatementEntity statement = new FinanceMoneyStatementEntity();
		statement.setReceiptAmount(BigDecimal.ZERO);
		statement.setFinanceId(pbwe.getFinanceId());
		statement.setFinanceNumber(pbwe.getFinanceNumber());
		statement.setReceiptType(OperatorRoleEnum.PLATFORM.ordinal());
		statement.setReceiptName(ConfigPropertites.getProperties("platform.name"));
		statement.setReceiptTenantId(FinanceConstant.DEFAULT_PLATFORM_WEB_TENANT);
		statement.setDisbursementType(OperatorRoleEnum.ENTERPRISE.ordinal());
		statement.setDisbursementName(pawe.getEnterpriseName());
		statement.setAmount(pbwe.getOthers());
		statement.setStatementState(StatementStateEnum.PAID);
		statement.setStatementSource("QUARTER_SETTLEMENT_OTHERS");
		statement.setStatementSourceId(pawe.getPickupId());
		statement.setSourceNumber(pawe.getPickupNumber());
		statement.setStatementDate(DateUtil.todayInt());
		statement.setDisbursementId(pawe.getEnterpriseId());
		return statement;
	}
	
	/** 其他应收应付 */
	public static FinanceMoneyStatementEntity fromOtherStatement(FinanceOtherMoneyStatementEntity otherStatement) {
		FinanceMoneyStatementEntity statement = new FinanceMoneyStatementEntity();
		statement.setReceiptType(otherStatement.getReceiptType());
		if (otherStatement.getReceiptType() == 1) {
			statement.setReceiptName(ConfigPropertites.getProperties("platform.name"));
			statement.setReceiptTenantId(FinanceConstant.DEFAULT_PLATFORM_WEB_TENANT);
		} else {
			statement.setReceiptName(otherStatement.getReceiptName());
			statement.setReceiptId(otherStatement.getReceiptId());
		}
		
		statement.setDisbursementType(otherStatement.getDisbursementType());
		if (otherStatement.getDisbursementType() == 1) {
			statement.setDisbursementName(ConfigPropertites.getProperties("platform.name"));
			statement.setDisbursementTenantId(FinanceConstant.DEFAULT_PLATFORM_WEB_TENANT);
		} else {
			statement.setDisbursementName(otherStatement.getDisbursementName());
			statement.setDisbursementId(otherStatement.getDisbursementId());
		}
		statement.setAmount(otherStatement.getAmount());
		statement.setStatementState(StatementStateEnum.PAID);
		statement.setStatementSource(otherStatement.getStatementSource());
		statement.setStatementDate(DateUtil.todayInt());
		return statement;
	}

	/** 保险理赔平台收款流水 */
	public static FinanceMoneyStatementEntity fromPlatformAgentInsuranceClaim(InsuranceEntity insurance, ClaimEntity claim, ClaimSettlementEntity settlement) {
		FinanceMoneyStatementEntity statement = new FinanceMoneyStatementEntity();
		statement.setFinanceId(claim.getFinanceId());
		statement.setFinanceNumber(claim.getFinanceNumber());
		statement.setReceiptType(OperatorRoleEnum.PLATFORM.ordinal());
		statement.setReceiptName(ConfigPropertites.getProperties("platform.name"));
		statement.setReceiptTenantId(FinanceConstant.DEFAULT_PLATFORM_WEB_TENANT);
		statement.setDisbursementType(OperatorRoleEnum.OTHER.ordinal());
		statement.setDisbursementName(insurance.getInsuranceCompany());
		statement.setAmount(settlement.getSettlementAmount());
		statement.setStatementState(StatementStateEnum.PAID);
		statement.setStatementSource("CLAIM_SETTLEMENT");
		statement.setStatementSourceId(settlement.getClaimSettlementId());
		statement.setSourceNumber(settlement.getSettlementNumber());
		statement.setStatementDate(DateUtil.todayInt());
		return statement;
	}

	/** 保险理赔平台付款流水 */
	public static FinanceMoneyStatementEntity fromEnterpriseAgentInsuranceClaim(InsuranceEntity insurance, ClaimEntity claim, ClaimSettlementEntity settlement) {
		FinanceMoneyStatementEntity statement = new FinanceMoneyStatementEntity();
		statement.setFinanceId(claim.getFinanceId());
		statement.setFinanceNumber(claim.getFinanceNumber());
		statement.setReceiptType(insurance.getMold().ordinal());
		statement.setReceiptName(insurance.getApplicantDepartment());
		statement.setReceiptId(insurance.getApplicantId());
		statement.setReceiptTenantId(insurance.getApplicantTenantId());
		statement.setDisbursementType(OperatorRoleEnum.PLATFORM.ordinal());
		statement.setDisbursementName(ConfigPropertites.getProperties("platform.name"));
		statement.setDisbursementTenantId(FinanceConstant.DEFAULT_PLATFORM_WEB_TENANT);
		statement.setAmount(settlement.getSettlementAmount());
		statement.setStatementState(StatementStateEnum.PAID);
		statement.setStatementSource("CLAIM_SETTLEMENT");
		statement.setStatementSourceId(settlement.getClaimSettlementId());
		statement.setSourceNumber(settlement.getSettlementNumber());
		statement.setStatementDate(DateUtil.parseDateInt(settlement.getSettlementDate()));
		return statement;
	}

	/** 保险理赔企业收款流水 */
	public static FinanceMoneyStatementEntity fromEnterpriseInsuranceClaim(InsuranceEntity insurance, ClaimEntity claim, ClaimSettlementEntity settlement) {
		FinanceMoneyStatementEntity statement = new FinanceMoneyStatementEntity();
		statement.setFinanceId(claim.getFinanceId());
		statement.setFinanceNumber(claim.getFinanceNumber());
		statement.setReceiptType(insurance.getMold().ordinal());
		statement.setReceiptName(insurance.getApplicantDepartment());
		statement.setReceiptId(insurance.getApplicantId());
		statement.setReceiptTenantId(insurance.getApplicantTenantId());
		statement.setDisbursementType(OperatorRoleEnum.OTHER.ordinal());
		statement.setDisbursementName(insurance.getInsuranceCompany());
		statement.setAmount(settlement.getSettlementAmount());
		statement.setStatementState(StatementStateEnum.PAID);
		statement.setStatementSource("CLAIM_SETTLEMENT");
		statement.setStatementSourceId(settlement.getClaimSettlementId());
		statement.setSourceNumber(settlement.getSettlementNumber());
		statement.setStatementDate(DateUtil.parseDateInt(settlement.getSettlementDate()));
		return statement;
	}

	/** 保险费用企业收款流水 */
	public static FinanceMoneyStatementEntity fromAgentInsurance(InsuranceEntity insurance) {
		FinanceMoneyStatementEntity statement = new FinanceMoneyStatementEntity();
		statement.setFinanceId(insurance.getFinanceId());
		statement.setFinanceNumber(insurance.getFinanceNumber());
		statement.setReceiptType(OperatorRoleEnum.PLATFORM.ordinal());
		statement.setReceiptName(ConfigPropertites.getProperties("platform.name"));
		statement.setReceiptTenantId(FinanceConstant.DEFAULT_PLATFORM_WEB_TENANT);
		statement.setDisbursementType(insurance.getMold().ordinal());
		statement.setDisbursementId(insurance.getApplicantId());
		statement.setDisbursementTenantId(insurance.getApplicantTenantId());
		statement.setDisbursementName(insurance.getApplicantDepartment());
		statement.setAmount(insurance.getTotalPremium());
		statement.setStatementState(StatementStateEnum.PAID);
		statement.setStatementSource("INSURANCE");
		statement.setStatementSourceId(insurance.getInsuranceId());
		statement.setSourceNumber(insurance.getInsuranceNumber());
		statement.setStatementDate(insurance.getInsuranceDate());
		return statement;
	}

	/** 保险费用企业付款流水 */
	public static FinanceMoneyStatementEntity fromInsurance(InsuranceEntity insurance) {
		FinanceMoneyStatementEntity statement = new FinanceMoneyStatementEntity();
		statement.setFinanceId(insurance.getFinanceId());
		statement.setFinanceNumber(insurance.getFinanceNumber());
		statement.setReceiptType(OperatorRoleEnum.OTHER.ordinal());
		statement.setReceiptName(insurance.getInsuranceCompany());
		statement.setDisbursementType(OperatorRoleEnum.PLATFORM.ordinal());
		statement.setDisbursementName(ConfigPropertites.getProperties("platform.name"));
		statement.setDisbursementTenantId(FinanceConstant.DEFAULT_PLATFORM_WEB_TENANT);
		statement.setAmount(insurance.getTotalPremium());
		statement.setStatementState(StatementStateEnum.PAID);
		statement.setStatementSource("INSURANCE");
		statement.setStatementSourceId(insurance.getInsuranceId());
		statement.setSourceNumber(insurance.getInsuranceNumber());
		statement.setStatementDate(insurance.getInsuranceDate());
		return statement;
	}

	public static FinanceMoneyStatementEntity fromCommidityLoanNotice(LoanNoticeEntity loanNotice) {
		EnterpriseService enterpriseService = ComponentFactory.loadClass(EnterpriseService.class);
		EnterpriseEntity enterprise = enterpriseService.inquireEnterprise(loanNotice.getEnterpriseId());
		FinanceApplicationService financeApplicationService = ComponentFactory.loadClass(FinanceApplicationService.class);
		SupplierService supplierService = ComponentFactory.loadClass(SupplierService.class);
		
		FinanceApplicationEntity finance = financeApplicationService.inquireFinanceApplicationEntity(loanNotice.getFinanceId());

		FinanceMoneyStatementEntity statement = new FinanceMoneyStatementEntity();
		
		if (ProductTypeEnum.PURCHASING == finance.getProductType() || ProductTypeEnum.PURCHASING_STORAGE == finance.getProductType()) {
			SupplierEntity supplier = supplierService.inquireSupplierDetail(finance.getReceive());
			statement.setReceiptType(OperatorRoleEnum.SUPPLIER.ordinal());
			statement.setReceiptName(supplier.getSupplierName());
			statement.setReceiptId(supplier.getSupplierId());
		} else {
			statement.setReceiptType(OperatorRoleEnum.ENTERPRISE.ordinal());
			statement.setReceiptName(loanNotice.getEnterpriseName());
			statement.setReceiptId(loanNotice.getEnterpriseId());
			statement.setReceiptTenantId(enterprise.getTenantId());
		}
		
		statement.setFinanceId(loanNotice.getFinanceId());
		statement.setFinanceNumber(loanNotice.getFinanceNumber());
		
		statement.setDisbursementType(OperatorRoleEnum.PLATFORM.ordinal());
		statement.setDisbursementName(ConfigPropertites.getProperties("platform.name"));
		statement.setDisbursementTenantId(FinanceConstant.DEFAULT_PLATFORM_WEB_TENANT);
		statement.setAmount(loanNotice.getLoanAmount());
		statement.setStatementState(StatementStateEnum.PAID);
		statement.setStatementSource("LOAN_NOTICE");
		statement.setStatementSourceId(loanNotice.getLoanNoticeId());
		statement.setSourceNumber(loanNotice.getLoanNumber());
		statement.setStatementDate(loanNotice.getLoanDate());
		return statement;
	}

	/** 生成收款单流水 */
	public static FinanceMoneyStatementEntity fromReceivables(ReceivablesEntity receivables) {
		EnterpriseService enterpriseService = ComponentFactory.loadClass(EnterpriseService.class);
		EnterpriseEntity enterprise = enterpriseService.inquireEnterprise(receivables.getEnterpriseId());

		FinanceMoneyStatementEntity statement = new FinanceMoneyStatementEntity();
		statement.setFinanceId(receivables.getFinanceId());
		statement.setFinanceNumber(receivables.getFinanceNumber());
		statement.setReceiptType(OperatorRoleEnum.PLATFORM.ordinal());
		statement.setReceiptName(ConfigPropertites.getProperties("platform.name"));
		statement.setReceiptTenantId(FinanceConstant.DEFAULT_PLATFORM_WEB_TENANT);
		statement.setDisbursementType(OperatorRoleEnum.ENTERPRISE.ordinal());
		statement.setDisbursementId(receivables.getEnterpriseId());
		statement.setDisbursementTenantId(enterprise.getTenantId());
		statement.setDisbursementName(receivables.getEnterpriseName());
		statement.setAmount(receivables.getBeneficiaryAmount());
		statement.setCreditQuota(receivables.getCreditQuota());
		statement.setStatementState(StatementStateEnum.PAID);
		statement.setStatementSource("RECEIVABLES");
		statement.setStatementSourceId(receivables.getReceivablesId());
		statement.setSourceNumber(receivables.getBeneficiaryNumber());
		statement.setStatementDate(receivables.getBeneficiaryDate());
		return statement;
	}

	// 生成企业收付款单
	public static BillReceivableEntity toBillReceivableEntity(BillTypeEnum type, FinanceWriteoffParams writeoff) {
		BillReceivableEntity entity = new BillReceivableEntity();
		entity.setDisbursementBank(writeoff.getDisbursementBank());
		entity.setDisbursementCreditCard(writeoff.getDisbursementCreditCard());
		entity.setDisbursementName(writeoff.getDisbursementName());
		entity.setDrawee(writeoff.getDrawee());
		entity.setReceiptBank(writeoff.getReceiptBank());
		entity.setReceiptCreditCard(writeoff.getReceiptCreditCard());
		entity.setReceiptName(writeoff.getReceiptName());
		entity.setPayee(writeoff.getPayee());
		entity.setAmount(writeoff.getWriteoffAmount());
		entity.setBalance(BigDecimal.ZERO);
		entity.setWriteoffNumber(writeoff.getWriteoffNumber());
		entity.setFileUuid(writeoff.getFileUuid());
		entity.setOperator(FinanceSessionContext.getAccountFullName());
		entity.setOperatorId(FinanceSessionContext.getAccountId());
		entity.setWriteoffAmount(writeoff.getWriteoffAmount());
		entity.setWriteoffDateTime(writeoff.getWriteoffDateTime());
		entity.setWriteoffRemark(writeoff.getWriteoffRemark());
		entity.setBillType(type);

		return entity;
	}

	/** 付款核销 */
	public static FinanceWriteoffEntity fromFinanceDisbursementWriteoffParams(FinanceWriteoffParams params) {
		FinanceWriteoffEntity entity = new FinanceWriteoffEntity();
		BeanUtils.copyProperties(params, entity);
		entity.setDisbursementId(FinanceSessionContext.getAccountId());
		entity.setDisbursementTenantId(FinanceSessionContext.getAccountTenantId());
		entity.setOperateDateTime(DateUtil.now());
		entity.setOperator(FinanceSessionContext.getAccountFullName());
		entity.setOperatorId(FinanceSessionContext.getAccountId());
		entity.setOperatorTenantId(FinanceSessionContext.getAccountTenantId());
		entity.setWriteoffType(WriteoffTypeEnum.DISBURSEMENT);
		return entity;
	}

	/** 收款核销 */
	public static FinanceWriteoffEntity fromFinanceReceiptWriteoffParams(FinanceWriteoffParams params) {
		FinanceWriteoffEntity entity = new FinanceWriteoffEntity();
		BeanUtils.copyProperties(params, entity);
		entity.setReceiptId(FinanceSessionContext.getAccountId());
		entity.setReceiptTenantId(FinanceSessionContext.getAccountTenantId());
		entity.setOperateDateTime(DateUtil.now());
		entity.setOperator(FinanceSessionContext.getAccountFullName());
		entity.setOperatorId(FinanceSessionContext.getAccountId());
		entity.setOperatorTenantId(FinanceSessionContext.getAccountTenantId());
		entity.setWriteoffType(WriteoffTypeEnum.RECEIPT);
		return entity;
	}

	/** 收付款核销流水信息 */
	public static List<FinanceWriteoffStatementEntity> fromFinanceWriteoffParams(FinanceWriteoffEntity writeoff, FinanceWriteoffParams params) {
		List<FinanceWriteoffStatementEntity> statements = new ArrayList<>();
		if (params.getMoneyStatementId() == null || params.getMoneyStatementId().length <= 0) {
			return statements;
		}
		for (int i = 0; i < params.getMoneyStatementId().length; i++) {
			Long moneyStatementId = params.getMoneyStatementId()[i];
			FinanceWriteoffStatementEntity entity = new FinanceWriteoffStatementEntity();
			entity.setMoneyStatementId(moneyStatementId);
			entity.setWriteoffId(writeoff.getWriteoffId());
			entity.setVerifiAmount(params.getVerifiAmount()[i]);
			statements.add(entity);
		}
		return statements;
	}

	/** 核销信息 */
	public static FinanceWriteoffLineInfo toFinanceWriteoffLineInfo(FinanceWriteoffEntity entity) {
		FinanceWriteoffLineInfo info = new FinanceWriteoffLineInfo();
		BeanUtils.copyProperties(entity, info);
		return info;
	}

	/** 核销明细 */
	public static FinanceWriteoffDetail toFinanceWriteoffDetail(FinanceWriteoffEntity entity) {
		FinanceWriteoffDetail info = new FinanceWriteoffDetail();
		BeanUtils.copyProperties(entity, info);
		FinanceWriteoffStatementService financeWriteoffStatementService = ComponentFactory.loadClass(FinanceWriteoffStatementService.class);
		List<FinanceWriteoffStatementEntity> writeoffStatements = financeWriteoffStatementService.inquireFinanceWriteoffStatementList(entity.getWriteoffId());
		Map<Long, FinanceWriteoffStatementEntity> moneyStatementIds = new HashMap<>();
		for (FinanceWriteoffStatementEntity fwse : writeoffStatements) {
			moneyStatementIds.put(fwse.getMoneyStatementId(), fwse);
		}
		FinanceMoneyStatementService financeMoneyStatementService = ComponentFactory.loadClass(FinanceMoneyStatementService.class);
		List<FinanceMoneyStatementEntity> statements = financeMoneyStatementService.inquireFinanceMoneyStatementList(moneyStatementIds.keySet());
		for (FinanceMoneyStatementEntity fmse : statements) {
			FinanceMoneyStatement fms = FinanceLoanCastor.toFinanceMoneyStatement(fmse);
			if (entity.getWriteoffType() == WriteoffTypeEnum.DISBURSEMENT) {
				fms.setCurrentDisbursementAmount(moneyStatementIds.get(fmse.getMoneyStatementId()).getVerifiAmount());
			} else {
				fms.setCurrentReceiptAmount(moneyStatementIds.get(fmse.getMoneyStatementId()).getVerifiAmount());
			}
			info.getStatements().add(fms);
		}
		return info;
	}

	/** 创建授信额度调整 */
	public static FinanceCreditEntity fromFinanceCredit(FinanceApplicationEntity finance, BigDecimal creditAmount, BigDecimal valuation, String remark, Integer reson) {
		FinanceCreditEntity entity = new FinanceCreditEntity();
		entity.setAdjustment(AdjustmentEnum.CREDIT);
		entity.setAuditState(AuditStateEnum.UNAUDIT);
		entity.setFinanceId(finance.getFinanceId());
		entity.setFinanceNumber(finance.getFinanceNumber());
		entity.setEnterpriseId(finance.getEnterpriseId());
		entity.setCreditQuota(finance.getCreditQuota());
		entity.setBail(finance.getBail());
		entity.setValuation(valuation);
		entity.setReson(reson);
		entity.setOperator(FinanceSessionContext.getAccountFullName());
		entity.setOperatorId(FinanceSessionContext.getAccountId());
		entity.setRemark(remark);
		entity.setCreditAmount(creditAmount);
		return entity;
	}

	/** 创建授信额度调整 */
	public static FinanceCreditBulkstockEntity fromFinanceCredit(FinanceBulkstockEntity fbe, Long creditId, Long bulkstockId, BigDecimal evaluateAmount) {
		FinanceCreditBulkstockEntity bulkstock = new FinanceCreditBulkstockEntity();
		bulkstock.setBulkstockId(bulkstockId);
		bulkstock.setBulkstockCategoryId(fbe.getBulkstockCategoryId());
		bulkstock.setBulkstockInfoId(fbe.getBulkstockInfoId());
		bulkstock.setBulkstockName(fbe.getBulkstockName());
		bulkstock.setSpecification(fbe.getSpecification());
		bulkstock.setCreditId(creditId);
		bulkstock.setEvaluatePrice(fbe.getEvaluatePrice());
		bulkstock.setEvaluateAmount(evaluateAmount);
		return bulkstock;
	}

	/** 创建保证金追加 */
	public static FinanceCreditEntity fromFinanceCreditDeposit(FinanceApplicationEntity finance, BigDecimal amount, String remark, Integer reson) {
		FinanceCreditEntity entity = new FinanceCreditEntity();
		entity.setAdjustment(AdjustmentEnum.DESPOIST);
		entity.setFinanceId(finance.getFinanceId());
		entity.setFinanceNumber(finance.getFinanceNumber());
		entity.setEnterpriseId(finance.getEnterpriseId());
		entity.setCreditQuota(finance.getCreditQuota());
		entity.setBail(finance.getBail() == null ? BigDecimal.ZERO : finance.getBail());
		entity.setValuation(finance.getValuation());
		entity.setReson(reson);
		entity.setOperator(FinanceSessionContext.getAccountFullName());
		entity.setOperatorId(FinanceSessionContext.getAccountId());
		entity.setRemark(remark);
		entity.setCreditAmount(amount);
		entity.setAuditState(AuditStateEnum.UNAUDIT);
		return entity;
	}

	/** 创建补货申请 */
	public static FinanceCreditEntity fromFinanceCreditReplenish(FinanceApplicationEntity finance, BigDecimal replenishAmount, BigDecimal valuation, String remark, Integer reson, Long loanNoticeId) {
		FinanceCreditEntity entity = new FinanceCreditEntity();
		entity.setAdjustment(AdjustmentEnum.REPLENISH);
		entity.setFinanceId(finance.getFinanceId());
		entity.setFinanceNumber(finance.getFinanceNumber());
		entity.setEnterpriseId(finance.getEnterpriseId());
		entity.setCreditAmount(replenishAmount); // 应补货金额
		entity.setValuation(valuation);
		entity.setReson(reson);
		entity.setReplenish(ReplenishEnum.WAIT); // 待补货
		entity.setReplenishMethod(ReplenishMethodEnum.UNKNOWN);
		entity.setLoanNoticeId(loanNoticeId);
		entity.setReplenishState(ReplenishStateEnum.WAIT); // 补货未确认
		entity.setOperator(FinanceSessionContext.getAccountFullName());
		entity.setOperatorId(FinanceSessionContext.getAccountId());
		entity.setRemark(remark);
		entity.setAuditState(AuditStateEnum.UNAUDIT);
		entity.setBail(BigDecimal.ZERO);
		return entity;
	}

	/** 补货提交 */
	public static FinanceCreditReplenishEntity fromFinanceCreditReplenishBulkstock(FinanceCreditEntity credit, BulkstockInfoEntity bulkstockInfo, BigDecimal bulkstockPrice, String manufacturer, String originPlace, BigDecimal weight) {
		FinanceCreditReplenishEntity replenish = new FinanceCreditReplenishEntity();
		replenish.setCreditId(credit.getCreditId());
		replenish.setBulkstockCategoryId(bulkstockInfo.getBulkstockCategoryId());
		replenish.setBulkstockId(null);
		replenish.setBulkstockInfoId(bulkstockInfo.getBulkstockInfoId());
		replenish.setBulkstockName(bulkstockInfo.getBulkstockName());
		replenish.setSpecification(bulkstockInfo.getSpecificationName());
		replenish.setBulkstockNumber(bulkstockInfo.getBulkstockNumber());
		replenish.setMeasurement(bulkstockInfo.getMeasurement());
		replenish.setBulkstockPrice(bulkstockPrice);
		replenish.setManufacturer(manufacturer);
		replenish.setOriginPlace(originPlace);
		replenish.setWeight(weight);
		replenish.setQuantity(replenish.getWeight());
		replenish.setUnit("批");
		replenish.setValuationDate(DateUtil.todayInt());
		replenish.setValuationQuantity(replenish.getWeight());
		replenish.setVolume(BigDecimal.ZERO);
		return replenish;
	}

	/** 补货提交-保存箱货信息 */
	public static FinanceCreditContainerBulkstockEntity fromFinanceReplenishContainer(FinanceCreditContainerEntity container, BulkstockInfoEntity bulkstockInfo, String trayNumber, BigDecimal weight, String originPlace,
			String manufacturer) {
		FinanceCreditContainerBulkstockEntity bulkstock = new FinanceCreditContainerBulkstockEntity();
		bulkstock.setContainerId(container.getContainerId());
		bulkstock.setBulkstockCategoryId(bulkstockInfo.getBulkstockCategoryId());
		bulkstock.setBulkstockInfoId(bulkstockInfo.getBulkstockInfoId());
		bulkstock.setBulkstockName(bulkstockInfo.getBulkstockName());
		bulkstock.setMeasurement(bulkstockInfo.getMeasurement());
		bulkstock.setSpecification(bulkstockInfo.getSpecificationName());
		bulkstock.setWeight(weight);
		bulkstock.setValuationQuantity(bulkstock.getWeight());
		bulkstock.setQuantity(bulkstock.getWeight());
		bulkstock.setTrayNumber(trayNumber);
		bulkstock.setUnit("批");
		bulkstock.setVolume(BigDecimal.ZERO);
		bulkstock.setManufacturer(manufacturer);
		bulkstock.setOriginPlace(originPlace);
		return bulkstock;
	}

	/** 补货提交-保存箱货信息-提取货信息 */
	public static FinanceCreditReplenishEntity fromFinanceReplenishContainer(BulkstockInfoEntity bulkstockInfo, FinanceCreditContainerEntity container, FinanceCreditContainerBulkstockEntity bulkstock) {
		FinanceCreditReplenishEntity replenish = new FinanceCreditReplenishEntity();
		replenish.setCreditId(container.getCreditId());
		replenish.setBulkstockCategoryId(bulkstockInfo.getBulkstockCategoryId());
		replenish.setBulkstockId(null);
		replenish.setBulkstockInfoId(bulkstockInfo.getBulkstockInfoId());
		replenish.setBulkstockName(bulkstockInfo.getBulkstockName());
		replenish.setSpecification(bulkstockInfo.getSpecificationName());
		replenish.setBulkstockNumber(bulkstockInfo.getBulkstockNumber());
		replenish.setMeasurement(bulkstockInfo.getMeasurement());
		replenish.setBulkstockPrice(BigDecimal.ZERO);
		replenish.setManufacturer(bulkstock.getManufacturer());
		replenish.setOriginPlace(bulkstock.getOriginPlace());
		replenish.setWeight(BigDecimal.ZERO);
		replenish.setQuantity(BigDecimal.ZERO);
		replenish.setUnit("批");
		replenish.setValuationDate(DateUtil.todayInt());
		replenish.setValuationQuantity(replenish.getWeight());
		replenish.setVolume(BigDecimal.ZERO);
		return replenish;
	}

	/**
	 * @param entity
	 * @param capital
	 * @return
	 */
	public static Capital toFinanceCapitalInfo(FinanceApplicationEntity finance, Capital capital) {
		FinanceAuditService financeAuditService = ComponentFactory.loadClass(FinanceAuditService.class);
		PickupApplicationService pickupApplicationService = ComponentFactory.loadClass(PickupApplicationService.class);
		FinanceMoneyStatementService financeMoneyStatementService = ComponentFactory.loadClass(FinanceMoneyStatementService.class);
		FinanceWriteoffStatementService financeWriteoffStatementService = ComponentFactory.loadClass(FinanceWriteoffStatementService.class);

		FinanceAuditEntity audit = financeAuditService.inquireLatestFinanceAudit(finance.getFinanceId());
		capital.setFinanceId(finance.getFinanceId());
		capital.setFinanceNumber(finance.getFinanceNumber());
		capital.setEnterpriseId(finance.getEnterpriseId());
		capital.setEnterpriseName(finance.getEnterpriseName());
		if (audit != null) {
			capital.setAuditDateTime(audit.getAuditDateTime());
		}

//		FinanceProductEntity product = financeProductService.inquireFinanceProduct(finance.getProductId());
//		capital.setProductId(product.getProductId());
//		capital.setProductName(product.getProductName());
//		capital.setMinDays(product.getMinDays());
//		capital.setMaxDays(product.getMaxDays());

		capital.setFinanceAmount(finance.getFinanceAmount());
		capital.setTotalLoanAmount(finance.getTotalLoanAmount()); // TODO 放款未走流水查询
		capital.setTotalWaitLoanAmount(capital.getFinanceAmount().subtract(capital.getTotalLoanAmount()));
		// 融资计息(现金提货)-收款单
		BigDecimal totalReceivedAmount = BigDecimal.ZERO; // 已收融资款总额
		BigDecimal interestAmount = BigDecimal.ZERO; // 融资计息-应收利息总额
		BigDecimal interestReceiveAmount = BigDecimal.ZERO; // 融资计息-已收利息总额

		String[] statementSources = new String[] { "RECEIVABLES" };
		List<FinanceMoneyStatementEntity> statements = financeMoneyStatementService.inquireFinanceMoneyReceiptStatementList(FinanceConstant.DEFAULT_PLATFORM_WEB_TENANT, finance.getFinanceId(), statementSources);
		for (FinanceMoneyStatementEntity s : statements) {
			PickupApplicationEntity pickup = pickupApplicationService.inquirePickupApplication(s.getStatementSourceId());
			if (pickup == null) {
				continue;
			}
			interestAmount = interestAmount.add(pickup.getBulkstockInterest());
			if (s.getReceiptVerifiState()) { // 已核销
				totalReceivedAmount = totalReceivedAmount.add(pickup.getFinanceValuation());
				interestReceiveAmount = interestReceiveAmount.add(pickup.getBulkstockInterest());
			} else { // 未核销
				List<FinanceWriteoffStatementEntity> writeoffs = financeWriteoffStatementService.inquireFinanceWriteoffMoneyStatementList(s.getMoneyStatementId());
				BigDecimal total = pickup.getFinanceValuation().add(pickup.getBulkstockInterest());
				BigDecimal temp = BigDecimal.ZERO;
				for (FinanceWriteoffStatementEntity writeoff : writeoffs) {
					temp = temp.add(writeoff.getVerifiAmount());
				}
				if (total.compareTo(BigDecimal.ZERO) == 0) {
					continue;
				}
				BigDecimal divide = temp.multiply(pickup.getFinanceValuation()).divide(total, 2, BigDecimal.ROUND_HALF_UP);
				totalReceivedAmount = totalReceivedAmount.add(divide);
				interestReceiveAmount = interestReceiveAmount.add(temp.subtract(divide));
			}
		}
		capital.setTotalReceivedAmount(totalReceivedAmount);
		capital.setTotalWaitReceivedAmount(capital.getFinanceAmount().subtract(capital.getTotalReceivedAmount()));

		statementSources = new String[] { "QUARTER_SETTLEMENT" };
		statements = financeMoneyStatementService.inquireFinanceMoneyReceiptStatementList(FinanceConstant.DEFAULT_PLATFORM_WEB_TENANT, finance.getFinanceId(), statementSources);
		for (FinanceMoneyStatementEntity s : statements) {
			interestAmount = interestAmount.add(s.getAmount());
			if (s.getReceiptVerifiState()) { // 已核销
				interestReceiveAmount = interestReceiveAmount.add(s.getAmount());
			} else { // 未核销
				List<FinanceWriteoffStatementEntity> writeoffs = financeWriteoffStatementService.inquireFinanceWriteoffMoneyStatementList(s.getMoneyStatementId());
				for (FinanceWriteoffStatementEntity writeoff : writeoffs) {
					interestReceiveAmount = interestReceiveAmount.add(writeoff.getVerifiAmount());
				}
			}
		}
		capital.setInterestAmount(interestAmount);
		capital.setInterestReceiveAmount(interestReceiveAmount);
		capital.setInterestWaitReceiveAmount(capital.getInterestAmount().subtract(capital.getInterestReceiveAmount()));
		// 保证金
		statementSources = new String[] { "FINANCE_AUDIT" };
		statements = financeMoneyStatementService.inquireFinanceMoneyReceiptStatementList(FinanceConstant.DEFAULT_PLATFORM_WEB_TENANT, finance.getFinanceId(), statementSources);
		BigDecimal receivableBail = BigDecimal.ZERO;
		BigDecimal receivedBail = BigDecimal.ZERO;
		for (FinanceMoneyStatementEntity s : statements) {
			receivableBail = receivableBail.add(s.getAmount());
			if (s.getReceiptVerifiState()) { // 已核销
				receivedBail = receivedBail.add(s.getAmount());
			} else { // 未核销
				List<FinanceWriteoffStatementEntity> writeoffs = financeWriteoffStatementService.inquireFinanceWriteoffMoneyStatementList(s.getMoneyStatementId());
				for (FinanceWriteoffStatementEntity writeoff : writeoffs) {
					receivedBail = receivedBail.add(writeoff.getVerifiAmount());
				}
			}
		}
		statementSources = new String[] { "BAIL_ADDITION" };
		statements = financeMoneyStatementService.inquireFinanceMoneyReceiptStatementList(FinanceConstant.DEFAULT_PLATFORM_WEB_TENANT, finance.getFinanceId(), statementSources);
		BigDecimal receivableAddBail = BigDecimal.ZERO;
		BigDecimal receivedAddBail = BigDecimal.ZERO;
		for (FinanceMoneyStatementEntity s : statements) {
			receivableAddBail = receivableAddBail.add(s.getAmount());
			if (s.getReceiptVerifiState()) { // 已核销
				receivedAddBail = receivedAddBail.add(s.getAmount());
			} else { // 未核销
				List<FinanceWriteoffStatementEntity> writeoffs = financeWriteoffStatementService.inquireFinanceWriteoffMoneyStatementList(s.getMoneyStatementId());
				for (FinanceWriteoffStatementEntity writeoff : writeoffs) {
					receivedAddBail = receivedAddBail.add(writeoff.getVerifiAmount());
				}
			}
		}
		statementSources = new String[] { "BAIL_RETURN" };
		statements = financeMoneyStatementService.inquireFinanceMoneyReceiptStatementList(FinanceConstant.DEFAULT_PLATFORM_WEB_TENANT, finance.getFinanceId(), statementSources);
		BigDecimal returnBail = BigDecimal.ZERO;
		for (FinanceMoneyStatementEntity s : statements) {
			if (s.getReceiptVerifiState()) { // 已核销
				returnBail = returnBail.add(s.getAmount());
			} else { // 未核销
				List<FinanceWriteoffStatementEntity> writeoffs = financeWriteoffStatementService.inquireFinanceWriteoffMoneyStatementList(s.getMoneyStatementId());
				for (FinanceWriteoffStatementEntity writeoff : writeoffs) {
					returnBail = returnBail.add(writeoff.getVerifiAmount());
				}
			}
		}
		capital.setReceivableBail(receivableBail);
		capital.setReceivedBail(receivedBail);
		capital.setReceivableAddBail(receivableAddBail);
		capital.setReceivedAddBail(receivedAddBail);
		capital.setReturnBail(returnBail);
		// 保险费用
		statementSources = new String[] { "INSURANCE", "CLAIM_SETTLEMENT" };
		statements = financeMoneyStatementService.inquireFinanceMoneyReceiptStatementList(FinanceConstant.DEFAULT_PLATFORM_WEB_TENANT, finance.getFinanceId(), statementSources);
		BigDecimal receivableInsureAmount = BigDecimal.ZERO;
		BigDecimal receivedInsureAmount = BigDecimal.ZERO;
		for (FinanceMoneyStatementEntity s : statements) {
			receivableInsureAmount = receivableInsureAmount.add(s.getAmount());
			if (s.getReceiptVerifiState()) { // 已核销
				receivedInsureAmount = receivedInsureAmount.add(s.getAmount());
			} else { // 未核销
				List<FinanceWriteoffStatementEntity> writeoffs = financeWriteoffStatementService.inquireFinanceWriteoffMoneyStatementList(s.getMoneyStatementId());
				for (FinanceWriteoffStatementEntity writeoff : writeoffs) {
					receivedInsureAmount = receivedInsureAmount.add(writeoff.getVerifiAmount());
				}
			}
		}
		statementSources = new String[] { "INSURANCE", "CLAIM_SETTLEMENT" };
		statements = financeMoneyStatementService.inquireFinanceMoneyDisbursementStatementList(FinanceConstant.DEFAULT_PLATFORM_WEB_TENANT, finance.getFinanceId(), statementSources);
		BigDecimal paymentableInsureAmount = BigDecimal.ZERO;
		BigDecimal paidInsureAmount = BigDecimal.ZERO;
		for (FinanceMoneyStatementEntity s : statements) {
			paymentableInsureAmount = paymentableInsureAmount.add(s.getAmount());
			if (s.getReceiptVerifiState()) { // 已核销
				paidInsureAmount = paidInsureAmount.add(s.getAmount());
			} else { // 未核销
				List<FinanceWriteoffStatementEntity> writeoffs = financeWriteoffStatementService.inquireFinanceWriteoffMoneyStatementList(s.getMoneyStatementId());
				for (FinanceWriteoffStatementEntity writeoff : writeoffs) {
					paidInsureAmount = paidInsureAmount.add(writeoff.getVerifiAmount());
				}
			}
		}
		capital.setReceivableInsureAmount(receivableInsureAmount);
		capital.setReceivedInsureAmount(receivedInsureAmount);
		capital.setPaymentableInsureAmount(paymentableInsureAmount);
		capital.setPaidInsureAmount(paidInsureAmount);
		// 其他应收
		statementSources = new String[] { "RECEIVABLES", "QUARTER_SETTLEMENT", "FINANCE_AUDIT", "BAIL_ADDITION", "INSURANCE", "CLAIM_SETTLEMENT" };
		statements = financeMoneyStatementService.inquireExceptFinanceMoneyReceiptStatementList(FinanceConstant.DEFAULT_PLATFORM_WEB_TENANT, finance.getFinanceId(), statementSources);
		BigDecimal receivableOtherAmount = BigDecimal.ZERO;
		BigDecimal receivedOtherAmount = BigDecimal.ZERO;
		for (FinanceMoneyStatementEntity s : statements) {
			receivableOtherAmount = receivableOtherAmount.add(s.getAmount());
			if (s.getReceiptVerifiState()) { // 已核销
				receivedOtherAmount = receivedOtherAmount.add(s.getAmount());
			} else { // 未核销
				List<FinanceWriteoffStatementEntity> writeoffs = financeWriteoffStatementService.inquireFinanceWriteoffMoneyStatementList(s.getMoneyStatementId());
				for (FinanceWriteoffStatementEntity writeoff : writeoffs) {
					receivedOtherAmount = receivedOtherAmount.add(writeoff.getVerifiAmount());
				}
			}
		}
		capital.setReceivableOtherAmount(receivableOtherAmount);
		capital.setReceivedOtherAmount(receivedOtherAmount);
		// 其他应付
		statementSources = new String[] { "BAIL_RETURN", "INSURANCE", "CLAIM_SETTLEMENT", "LOAN_NOTICE" };
		statements = financeMoneyStatementService.inquireExceptFinanceMoneyDisbursementStatementList(FinanceConstant.DEFAULT_PLATFORM_WEB_TENANT, finance.getFinanceId(), statementSources);
		BigDecimal paymentableOtherAmount = BigDecimal.ZERO;
		BigDecimal paidOtherAmount = BigDecimal.ZERO;
		for (FinanceMoneyStatementEntity s : statements) {
			paymentableOtherAmount = paymentableOtherAmount.add(s.getAmount());
			if (s.getReceiptVerifiState()) { // 已核销
				paidOtherAmount = paidOtherAmount.add(s.getAmount());
			} else { // 未核销
				List<FinanceWriteoffStatementEntity> writeoffs = financeWriteoffStatementService.inquireFinanceWriteoffMoneyStatementList(s.getMoneyStatementId());
				for (FinanceWriteoffStatementEntity writeoff : writeoffs) {
					paidOtherAmount = paidOtherAmount.add(writeoff.getVerifiAmount());
				}
			}
		}
		capital.setPaymentableOtherAmount(paymentableOtherAmount);
		capital.setPaidOtherAmount(paidOtherAmount);
		return capital;
	}

}
